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){
if l.level >= ERROR{
switch l.output {
case 'terminal':
match l.output {
'terminal'{
f := term.red('E')
t := time.now()
println('[$f ${t.format_ss()}] $s')
default:
} else {
l.log_file(s, 'E')
}
}
}
}
pub fn (l Log) warn(s string){
if l.level >= WARN{
switch l.output {
case 'terminal':
match l.output {
'terminal'{
f := term.yellow('W')
t := time.now()
println('[$f ${t.format_ss()}] $s')
default:
} else {
l.log_file(s, 'W')
}
}
}
}
pub fn (l Log) info(s string){
if l.level >= INFO{
switch l.output {
case 'terminal':
match l.output {
'terminal'{
f := term.white('I')
t := time.now()
println('[$f ${t.format_ss()}] $s')
default:
} else {
l.log_file(s, 'I')
}
}
}
}
pub fn (l Log) debug(s string){
if l.level >= DEBUG{
switch l.output {
case 'terminal':
match l.output {
'terminal' {
f := term.blue('D')
t := time.now()
println('[$f ${t.format_ss()}] $s')
default:
} else {
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
// escape single quote to avoid breaking callers that had previously assumed that
// single quotes would be escaped. See issue #19917.
switch c {
case `!`, `(`, `)`, `*`:
match c {
`!`, `(`, `)`, `*`{
return false
}
}
}
// Everything else must be escaped.
return true
@ -153,8 +154,8 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut has_plus := false
for i := 0; i < s.len; {
x := s[i]
switch x {
case `%`:
match x {
`%` {
if s == '' {
break
}
@ -189,16 +190,19 @@ fn unescape(s_ string, mode EncodingMode) ?string {
}
}
i += 3
case `+`:
}
`+`{
has_plus = mode == .encode_query_component
i++
default:
} else {
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)))
}
i++
}
}
}
if n == 0 && !has_plus {
return s
@ -207,20 +211,22 @@ fn unescape(s_ string, mode EncodingMode) ?string {
mut t := strings.new_builder(s.len - 2*n)
for i := 0; i < s.len; i++ {
x := s[i]
switch x {
case `%`:
match x {
`%` {
t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() )
i += 2
case `+`:
}
`+` {
if mode == .encode_query_component {
t.write(' ')
} else {
t.write('+')
}
default:
} else {
t.write(s[i].str())
}
}
}
return t.str()
}
@ -694,19 +700,22 @@ fn valid_encoded_path(s string) bool {
// so we check the sub-delims ourselves and let
// should_escape handle the others.
x := s[i]
switch x {
case `!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@`:
match x {
`!`, `$`, `&`, `\\`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, `:`, `@` {
// ok
case `[`, `]`:
}
`[`, `]` {
// ok - not specified in RFC 3986 but left alone by modern browsers
case `%`:
}
`%` {
// ok - percent encoded, will decode
default:
} else {
if should_escape(s[i], .encode_path) {
return false
}
}
}
}
return true
}
@ -914,17 +923,19 @@ fn resolve_path(base, ref string) string {
mut dst := []string
src := full.split('/')
for _, elem in src {
switch elem {
case '.':
match elem {
'.' {
// drop
case '..':
}
'..' {
if dst.len > 0 {
dst = dst.left(dst.len-1)
}
default:
} else {
dst << elem
}
}
}
last := src[src.len-1]
if last == '.' || last == '..' {
// Add final slash to the joined path.
@ -1069,14 +1080,15 @@ pub fn valid_userinfo(s string) bool {
if `0` <= r && r <= `9` {
continue
}
switch r {
case `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`,
`(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@`:
match r {
`-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`,
`(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@` {
continue
default:
} else {
return false
}
}
}
return true
}