mirror of
https://github.com/vlang/v.git
synced 2025-08-03 09:47:15 -04:00
vlib: simplify byte character conditions by using methods like is_capital, is_lower, is_letter etc (#21725)
This commit is contained in:
parent
a536c03365
commit
5b9358279a
@ -39,7 +39,7 @@ pub fn (c u8) is_digit() bool {
|
||||
// Example: assert u8(`F`) == true
|
||||
@[inline]
|
||||
pub fn (c u8) is_hex_digit() bool {
|
||||
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
||||
return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
||||
}
|
||||
|
||||
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
||||
|
@ -952,7 +952,7 @@ pub fn (s string) is_capital() bool {
|
||||
// Example: assert 'Hello. World.'.starts_with_capital() == true
|
||||
@[direct_array_access]
|
||||
pub fn (s string) starts_with_capital() bool {
|
||||
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
|
||||
if s.len == 0 || !s[0].is_capital() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -1518,7 +1518,7 @@ pub fn (s string) to_lower() string {
|
||||
unsafe {
|
||||
mut b := malloc_noscan(s.len + 1)
|
||||
for i in 0 .. s.len {
|
||||
if s.str[i] >= `A` && s.str[i] <= `Z` {
|
||||
if s.str[i].is_capital() {
|
||||
b[i] = s.str[i] + 32
|
||||
} else {
|
||||
b[i] = s.str[i]
|
||||
@ -1637,7 +1637,7 @@ pub fn (s string) is_capital() bool {
|
||||
// Example: assert 'Hello. World.'.starts_with_capital() == true
|
||||
@[direct_array_access]
|
||||
pub fn (s string) starts_with_capital() bool {
|
||||
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
|
||||
if s.len == 0 || !s[0].is_capital() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@ -2043,7 +2043,7 @@ pub fn (c u8) is_digit() bool {
|
||||
// Example: assert u8(`F`).is_hex_digit() == true
|
||||
@[inline]
|
||||
pub fn (c u8) is_hex_digit() bool {
|
||||
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
||||
return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
||||
}
|
||||
|
||||
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
||||
@ -2652,9 +2652,9 @@ pub fn (s string) camel_to_snake() string {
|
||||
}
|
||||
mut b := unsafe { malloc_noscan(2 * s.len + 1) }
|
||||
mut prev_is_upper := false
|
||||
first_char, second_char := if s[0] >= `A` && s[0] <= `Z` {
|
||||
lower_first_c := if s[0] >= `A` && s[0] <= `Z` { s[0] + 32 } else { s[0] }
|
||||
lower_second_c := if s[1] >= `A` && s[1] <= `Z` {
|
||||
first_char, second_char := if s[0].is_capital() {
|
||||
lower_first_c := s[0] + 32
|
||||
lower_second_c := if s[1].is_capital() {
|
||||
prev_is_upper = true
|
||||
s[1] + 32
|
||||
} else {
|
||||
@ -2663,7 +2663,7 @@ pub fn (s string) camel_to_snake() string {
|
||||
lower_first_c, lower_second_c
|
||||
} else {
|
||||
lower_first_c := s[0]
|
||||
second_c := if s[1] >= `A` && s[1] <= `Z` { u8(`_`) } else { s[1] }
|
||||
second_c := if s[1].is_capital() { u8(`_`) } else { s[1] }
|
||||
lower_first_c, second_c
|
||||
}
|
||||
unsafe {
|
||||
@ -2676,7 +2676,7 @@ pub fn (s string) camel_to_snake() string {
|
||||
mut c_is_upper := false
|
||||
for i in pos .. s.len {
|
||||
c := s[i]
|
||||
c_is_upper = c >= `A` && c <= `Z`
|
||||
c_is_upper = c.is_capital()
|
||||
lower_c = if c_is_upper { c + 32 } else { c }
|
||||
if !prev_is_upper && c_is_upper {
|
||||
// aB => a_b, if prev has `_`, then do not add `_`
|
||||
|
@ -248,7 +248,7 @@ pub fn is_cookie_domain_name(_s string) bool {
|
||||
mut part_len := 0
|
||||
for i, _ in s {
|
||||
c := s[i]
|
||||
if (`a` <= c && c <= `z`) || (`A` <= c && c <= `Z`) {
|
||||
if c.is_letter() {
|
||||
// No '_' allowed here (in contrast to package net).
|
||||
ok = true
|
||||
part_len++
|
||||
|
@ -38,7 +38,7 @@ fn error_msg(message string, val string) string {
|
||||
// reserved characters correctly. See golang.org/issue/5684.
|
||||
fn should_escape(c u8, mode EncodingMode) bool {
|
||||
// §2.3 Unreserved characters (alphanum)
|
||||
if (`a` <= c && c <= `z`) || (`A` <= c && c <= `Z`) || (`0` <= c && c <= `9`) {
|
||||
if c.is_alnum() {
|
||||
return false
|
||||
}
|
||||
if mode == .encode_host || mode == .encode_zone {
|
||||
@ -388,9 +388,9 @@ fn (u &Userinfo) str() string {
|
||||
fn split_by_scheme(rawurl string) ![]string {
|
||||
for i in 0 .. rawurl.len {
|
||||
c := rawurl[i]
|
||||
if (`a` <= c && c <= `z`) || (`A` <= c && c <= `Z`) {
|
||||
if c.is_letter() {
|
||||
// do nothing
|
||||
} else if (`0` <= c && c <= `9`) || (c == `+` || c == `-` || c == `.`) {
|
||||
} else if c.is_digit() || c in [`+`, `-`, `.`] {
|
||||
if i == 0 {
|
||||
return ['', rawurl]
|
||||
}
|
||||
@ -1016,13 +1016,7 @@ pub fn split_host_port(hostport string) (string, string) {
|
||||
// It doesn't validate pct-encoded. The caller does that via fn unescape.
|
||||
pub fn valid_userinfo(s string) bool {
|
||||
for r in s {
|
||||
if `A` <= r && r <= `Z` {
|
||||
continue
|
||||
}
|
||||
if `a` <= r && r <= `z` {
|
||||
continue
|
||||
}
|
||||
if `0` <= r && r <= `9` {
|
||||
if r.is_alnum() {
|
||||
continue
|
||||
}
|
||||
match r {
|
||||
|
@ -134,7 +134,7 @@ pub fn split_capital(s string) []string {
|
||||
mut res := []string{}
|
||||
mut word_start := 0
|
||||
for idx, c in s {
|
||||
if c >= `A` && c <= `Z` {
|
||||
if c.is_capital() {
|
||||
if word_start != idx {
|
||||
res << s#[word_start..idx]
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ module util
|
||||
// is_key_char returns true if the given u8 is a valid key character.
|
||||
@[inline]
|
||||
pub fn is_key_char(c u8) bool {
|
||||
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) // || c == `_` || c == `-` <- these are identified when tokenizing
|
||||
return c.is_letter() // || c == `_` || c == `-` <- these are identified when tokenizing
|
||||
}
|
||||
|
||||
// is_ascii_control_character returns true if `byte_char` is an ASCII control character.
|
||||
|
@ -239,7 +239,7 @@ fn (mut s Scanner) ident_name() string {
|
||||
s.pos++
|
||||
for s.pos < s.text.len {
|
||||
c := s.text[s.pos]
|
||||
if (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`) || c == `_` {
|
||||
if c.is_alnum() || c == `_` {
|
||||
s.pos++
|
||||
continue
|
||||
}
|
||||
|
@ -2,17 +2,17 @@ module util
|
||||
|
||||
@[inline]
|
||||
pub fn is_name_char(c u8) bool {
|
||||
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || c == `_`
|
||||
return c.is_letter() || c == `_`
|
||||
}
|
||||
|
||||
@[inline]
|
||||
pub fn is_func_char(c u8) bool {
|
||||
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || c == `_` || (c >= `0` && c <= `9`)
|
||||
return c.is_letter() || c == `_` || c.is_digit()
|
||||
}
|
||||
|
||||
pub fn contains_capital(s string) bool {
|
||||
for c in s {
|
||||
if c >= `A` && c <= `Z` {
|
||||
if c.is_capital() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -131,8 +131,7 @@ pub fn (mut s Scanner) scan() token.Token {
|
||||
}
|
||||
for s.offset < s.src.len {
|
||||
c3 := s.src[s.offset]
|
||||
if (c3 >= `a` && c3 <= `z`) || (c3 >= `A` && c3 <= `Z`)
|
||||
|| (c3 >= `0` && c3 <= `9`) || c3 == `_` {
|
||||
if c3.is_alnum() || c3 == `_` {
|
||||
s.offset++
|
||||
continue
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user