First stab for strconv
This will later be extended to be a real parser. Applied assorted tweaks
This commit is contained in:
parent
6730efb0f5
commit
c71d8e1495
2
Makefile
2
Makefile
|
@ -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
2
TODO
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Binary file not shown.
|
@ -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
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue