From ed558266867e3b9d4c92b5e8070b593c18378814 Mon Sep 17 00:00:00 2001 From: Don Alfons Nisnoni Date: Sun, 27 Oct 2019 14:45:03 +0800 Subject: [PATCH] log/net: switch => match --- vlib/log/log.v | 66 +++++++-------- vlib/net/urllib/urllib.v | 168 +++++++++++++++++++++------------------ 2 files changed, 123 insertions(+), 111 deletions(-) diff --git a/vlib/log/log.v b/vlib/log/log.v index 984fe770bc..7f0886003c 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -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': - f := term.red('E') - t := time.now() - println('[$f ${t.format_ss()}] $s') - - default: - l.log_file(s, 'E') + match l.output { + 'terminal'{ + f := term.red('E') + t := time.now() + println('[$f ${t.format_ss()}] $s') + } else { + l.log_file(s, 'E') + } } } } pub fn (l Log) warn(s string){ if l.level >= WARN{ - switch l.output { - case 'terminal': - f := term.yellow('W') - t := time.now() - println('[$f ${t.format_ss()}] $s') - - default: - l.log_file(s, 'W') + match l.output { + 'terminal'{ + f := term.yellow('W') + t := time.now() + println('[$f ${t.format_ss()}] $s') + } else { + l.log_file(s, 'W') + } } - } + } } pub fn (l Log) info(s string){ if l.level >= INFO{ - switch l.output { - case 'terminal': - f := term.white('I') - t := time.now() - println('[$f ${t.format_ss()}] $s') - - default: - l.log_file(s, 'I') + match l.output { + 'terminal'{ + f := term.white('I') + t := time.now() + println('[$f ${t.format_ss()}] $s') + } else { + l.log_file(s, 'I') + } } } } pub fn (l Log) debug(s string){ if l.level >= DEBUG{ - switch l.output { - case 'terminal': - f := term.blue('D') - t := time.now() - println('[$f ${t.format_ss()}] $s') - - default: - l.log_file(s, 'D') + match l.output { + 'terminal' { + f := term.blue('D') + t := time.now() + println('[$f ${t.format_ss()}] $s') + } else { + l.log_file(s, 'D') + } } } } diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index e22fe7a4b7..1e1f07754e 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -114,9 +114,10 @@ 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 `!`, `(`, `)`, `*`: - return false + match c { + `!`, `(`, `)`, `*`{ + return false + } } } @@ -153,50 +154,53 @@ fn unescape(s_ string, mode EncodingMode) ?string { mut has_plus := false for i := 0; i < s.len; { x := s[i] - switch x { - case `%`: - if s == '' { - break - } - n++ - if i+2 >= s.len || !ishex(s[i+1]) || !ishex(s[i+2]) { - s = s.right(i) - if s.len > 3 { - s = s.left(3) + match x { + `%` { + if s == '' { + break } - return error(error_msg(err_msg_escape, s)) - } - // Per https://tools.ietf.org/html/rfc3986#page-21 - // in the host component %-encoding can only be used - // for non-ASCII bytes. - // But https://tools.ietf.org/html/rfc6874#section-2 - // introduces %25 being allowed to escape a percent sign - // in IPv6 scoped-address literals. Yay. - if mode == .encode_host && unhex(s[i+1]) < 8 && s.substr(i, i+3) != '%25' { - return error(error_msg(err_msg_escape, s.substr(i, i+3))) - } - if mode == .encode_zone { - // RFC 6874 says basically 'anything goes' for zone identifiers - // and that even non-ASCII can be redundantly escaped, - // but it seems prudent to restrict %-escaped bytes here to those - // that are valid host name bytes in their unescaped form. - // That is, you can use escaping in the zone identifier but not - // to introduce bytes you couldn't just write directly. - // But Windows puts spaces here! Yay. - v := byte(unhex(s[i+1])<= s.len || !ishex(s[i+1]) || !ishex(s[i+2]) { + s = s.right(i) + if s.len > 3 { + s = s.left(3) + } + return error(error_msg(err_msg_escape, s)) } + // Per https://tools.ietf.org/html/rfc3986#page-21 + // in the host component %-encoding can only be used + // for non-ASCII bytes. + // But https://tools.ietf.org/html/rfc6874#section-2 + // introduces %25 being allowed to escape a percent sign + // in IPv6 scoped-address literals. Yay. + if mode == .encode_host && unhex(s[i+1]) < 8 && s.substr(i, i+3) != '%25' { + return error(error_msg(err_msg_escape, s.substr(i, i+3))) + } + if mode == .encode_zone { + // RFC 6874 says basically 'anything goes' for zone identifiers + // and that even non-ASCII can be redundantly escaped, + // but it seems prudent to restrict %-escaped bytes here to those + // that are valid host name bytes in their unescaped form. + // That is, you can use escaping in the zone identifier but not + // to introduce bytes you couldn't just write directly. + // But Windows puts spaces here! Yay. + v := byte(unhex(s[i+1])< 0 { - dst = dst.left(dst.len-1) + match elem { + '.' { + // drop + } + '..' { + if dst.len > 0 { + dst = dst.left(dst.len-1) + } + } else { + dst << elem } - default: - dst << elem } } last := src[src.len-1] @@ -1069,12 +1080,13 @@ pub fn valid_userinfo(s string) bool { if `0` <= r && r <= `9` { continue } - switch r { - case `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`, - `(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@`: - continue - default: - return false + match r { + `-`, `.`, `_`, `:`, `~`, `!`, `$`, `&`, `\\`, + `(`, `)`, `*`, `+`, `,`, `;`, `=`, `%`, `@` { + continue + } else { + return false + } } } return true