First stab for strconv

This will later be extended to be a real parser.
Applied assorted tweaks
This commit is contained in:
Miek Gieben 2010-12-30 20:00:26 +01:00
parent 6730efb0f5
commit c71d8e1495
13 changed files with 128 additions and 1 deletions

View File

@ -21,11 +21,13 @@ all: package
install: $(INSTALLFILES)
gomake -C dnssec install
gomake -C resolver install
# gomake -C strconv install
dnstest:
gotest
gomake -C dnssec test
gomake -C resolver test
# gomake -C strconv test
_examples:
gomake -C _examples

2
TODO
View File

@ -6,6 +6,8 @@ Todo:
* IDN?
* Unknown RRs?
* query-time, server in string ouput of dns.Msg
* Parsing from strings
* Server support (signing, receiving queries)
Issues:
* Better sized buffers

View File

@ -11,7 +11,7 @@ import (
)
func main() {
var dnssec *bool = flag.Bool("dnssec", false, "Set DO flag; set bufsize to 4096")
var dnssec *bool = flag.Bool("dnssec", false, "Request DNSSEC records")
var port *string = flag.String("port", "53", "Set the query port")
var aa *bool = flag.Bool("aa", false, "Set AA flag in query")
var ad *bool = flag.Bool("ad", false, "Set AD flag in query")

BIN
strconv/6.out Executable file

Binary file not shown.

11
strconv/Makefile Normal file
View File

@ -0,0 +1,11 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
include $(GOROOT)/src/Make.inc
TARG=dns/strconv
GOFILES=\
strconv.go\
include $(GOROOT)/src/Make.pkg

BIN
strconv/_go_.6 Normal file

Binary file not shown.

BIN
strconv/_gotest_.6 Normal file

Binary file not shown.

BIN
strconv/_obj/dns/strconv.a Normal file

Binary file not shown.

BIN
strconv/_test/dns/strconv.a Normal file

Binary file not shown.

BIN
strconv/_testmain.6 Normal file

Binary file not shown.

15
strconv/_testmain.go Normal file
View File

@ -0,0 +1,15 @@
package main
import "dns/strconv"
import "testing"
import __regexp__ "regexp"
var tests = []testing.InternalTest{
{"strconv.TestConversion", strconv.TestConversion},
}
var benchmarks = []testing.InternalBenchmark{}
func main() {
testing.Main(__regexp__.MatchString, tests)
testing.RunBenchmarks(__regexp__.MatchString, benchmarks)
}

73
strconv/strconv.go Normal file
View File

@ -0,0 +1,73 @@
package strconv
import (
"unicode"
conv "strconv"
)
const (
m = 60
h = m * m
d = 24 * h
w = 7 * d
)
// Convert a Ttl to a value. Supported value 'm' for minutes, 'h' for hours
// 'w' for week and 'd' for days. Stuff like '1d1d' is legal and return the value of '2d'
func StringToSeconds(ttl string) (sec uint32, ok bool) {
num := ""
for _, k := range ttl {
if unicode.IsDigit(k) {
num += string(k)
} else {
i, _ := conv.Atoi(num)
switch k {
case 'm':
sec += uint32(i) * m
case 'h':
sec += uint32(i) * h
case 'd':
sec += uint32(i) * d
case 'w':
sec += uint32(i) * w
default:
return
}
num = ""
}
}
i, _ := conv.Atoi(num)
sec += uint32(i)
return
}
func SecondsToString(val uint32) (str string) {
mod := val / w
if mod > 0 {
str += conv.Itoa(int(mod)) + "w"
}
val -= mod * w
mod = val / d
if mod > 0 {
str += conv.Itoa(int(mod)) + "d"
}
val -= mod * d
mod = val / h
if mod > 0 {
str += conv.Itoa(int(mod)) + "h"
}
val -= mod * h
mod = val / m
if mod > 0 {
str += conv.Itoa(int(mod)) + "m"
}
val -= mod * m
if val > 0 {
str += conv.Itoa(int(val))
}
return
}

24
strconv/ttl_test.go Normal file
View File

@ -0,0 +1,24 @@
package strconv
import ( "testing")
func TestConversion(t *testing.T) {
/*
println(StringToSeconds("6w8d50"))
println(StringToSeconds("50"))
println(StringToSeconds("1m1m"))
println(StringToSeconds("1w"))
println(StringToSeconds("1d"))
println(StringToSeconds("2d"))
println(StringToSeconds("1d1d"))
*/
println(SecondsToString(604800)) // 1w
println(SecondsToString(604799)) // 1w-1
println(SecondsToString(86400)) // 1d
println(SecondsToString(86401)) // 1d+1
println(SecondsToString(86399)) // 1d-1
println(SecondsToString(86)) // 1m26
println(SecondsToString(60)) // 1m
println(SecondsToString(59)) // 59
}