log/net: switch => match

pull/2560/head
Don Alfons Nisnoni 2019-10-27 14:45:03 +08:00 committed by Alexander Medvednikov
parent 7ba5248e56
commit ed55826686
2 changed files with 123 additions and 111 deletions

View File

@ -50,56 +50,56 @@ pub fn (l Log) fatal(s string){
pub fn (l Log) error(s string){ pub fn (l Log) error(s string){
if l.level >= ERROR{ if l.level >= ERROR{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.red('E') f := term.red('E')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default:
l.log_file(s, 'E') l.log_file(s, 'E')
} }
} }
}
} }
pub fn (l Log) warn(s string){ pub fn (l Log) warn(s string){
if l.level >= WARN{ if l.level >= WARN{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.yellow('W') f := term.yellow('W')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default:
l.log_file(s, 'W') l.log_file(s, 'W')
} }
} }
}
} }
pub fn (l Log) info(s string){ pub fn (l Log) info(s string){
if l.level >= INFO{ if l.level >= INFO{
switch l.output { match l.output {
case 'terminal': 'terminal'{
f := term.white('I') f := term.white('I')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default:
l.log_file(s, 'I') l.log_file(s, 'I')
} }
} }
}
} }
pub fn (l Log) debug(s string){ pub fn (l Log) debug(s string){
if l.level >= DEBUG{ if l.level >= DEBUG{
switch l.output { match l.output {
case 'terminal': 'terminal' {
f := term.blue('D') f := term.blue('D')
t := time.now() t := time.now()
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} else {
default:
l.log_file(s, 'D') l.log_file(s, 'D')
} }
} }
}
} }

View File

@ -114,11 +114,12 @@ fn should_escape(c byte, mode EncodingMode) bool {
// (1) we always escape sub-delims outside of the fragment, and (2) we always // (1) we always escape sub-delims outside of the fragment, and (2) we always
// escape single quote to avoid breaking callers that had previously assumed that // escape single quote to avoid breaking callers that had previously assumed that
// single quotes would be escaped. See issue #19917. // single quotes would be escaped. See issue #19917.
switch c { match c {
case `!`, `(`, `)`, `*`: `!`, `(`, `)`, `*`{
return false return false
} }
} }
}
// Everything else must be escaped. // Everything else must be escaped.
return true return true
@ -153,8 +154,8 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut has_plus := false mut has_plus := false
for i := 0; i < s.len; { for i := 0; i < s.len; {
x := s[i] x := s[i]
switch x { match x {
case `%`: `%` {
if s == '' { if s == '' {
break break
} }
@ -189,16 +190,19 @@ fn unescape(s_ string, mode EncodingMode) ?string {
} }
} }
i += 3 i += 3
case `+`: }
`+`{
has_plus = mode == .encode_query_component has_plus = mode == .encode_query_component
i++ i++
default: } else {
if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) { if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) {
error(error_msg('invalid character in host name', s.substr(i, i+1))) error(error_msg('invalid character in host name', s.substr(i, i+1)))
} }
i++ i++
} }
} }
}
if n == 0 && !has_plus { if n == 0 && !has_plus {
return s return s
@ -207,20 +211,22 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut t := strings.new_builder(s.len - 2*n) mut t := strings.new_builder(s.len - 2*n)
for i := 0; i < s.len; i++ { for i := 0; i < s.len; i++ {
x := s[i] x := s[i]
switch x { match x {
case `%`: `%` {
t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() ) t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() )
i += 2 i += 2
case `+`: }
`+` {
if mode == .encode_query_component { if mode == .encode_query_component {
t.write(' ') t.write(' ')
} else { } else {
t.write('+') t.write('+')
} }
default: } else {
t.write(s[i].str()) t.write(s[i].str())
} }
} }
}
return t.str() return t.str()
} }
@ -694,19 +700,22 @@ fn valid_encoded_path(s string) bool {
// so we check the sub-delims ourselves and let // so we check the sub-delims ourselves and let
// should_escape handle the others. // should_escape handle the others.
x := s[i] x := s[i]
switch x { match x {
case `!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@`: `!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@` {
// ok // ok
case `[`, `]`: }
`[`, `]` {
// ok - not specified in RFC 3986 but left alone by modern browsers // ok - not specified in RFC 3986 but left alone by modern browsers
case `%`: }
`%` {
// ok - percent encoded, will decode // ok - percent encoded, will decode
default: } else {
if should_escape(s[i], .encode_path) { if should_escape(s[i], .encode_path) {
return false return false
} }
} }
} }
}
return true return true
} }
@ -914,17 +923,19 @@ fn resolve_path(base, ref string) string {
mut dst := []string mut dst := []string
src := full.split('/') src := full.split('/')
for _, elem in src { for _, elem in src {
switch elem { match elem {
case '.': '.' {
// drop // drop
case '..': }
'..' {
if dst.len > 0 { if dst.len > 0 {
dst = dst.left(dst.len-1) dst = dst.left(dst.len-1)
} }
default: } else {
dst << elem dst << elem
} }
} }
}
last := src[src.len-1] last := src[src.len-1]
if last == '.' || last == '..' { if last == '.' || last == '..' {
// Add final slash to the joined path. // Add final slash to the joined path.
@ -1069,14 +1080,15 @@ pub fn valid_userinfo(s string) bool {
if `0` <= r && r <= `9` { if `0` <= r && r <= `9` {
continue continue
} }
switch r { match r {
case `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`, `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`,
`(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@`: `(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@` {
continue continue
default: } else {
return false return false
} }
} }
}
return true return true
} }