string: add is_upper/is_lower and fix parser.match_expr error

* string: add is_upper/is_lower and fix parser.match_expr error

* to_capital => capitalize, to_title => title

* is_titled => is_title, is_capitalized => is_capital
pull/4364/head
yuyi 2020-04-12 18:09:05 +08:00 committed by GitHub
parent db2c656da8
commit 0ced7116b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 37 deletions

View File

@ -744,6 +744,15 @@ pub fn (s string) to_lower() string {
return tos(b, s.len) return tos(b, s.len)
} }
pub fn (s string) is_lower() bool {
for i in 0..s.len {
if s[i] >= `A` && s[i] <= `Z` {
return false
}
}
return true
}
pub fn (s string) to_upper() string { pub fn (s string) to_upper() string {
mut b := malloc(s.len + 1) mut b := malloc(s.len + 1)
for i in 0..s.len { for i in 0..s.len {
@ -752,6 +761,15 @@ pub fn (s string) to_upper() string {
return tos(b, s.len) return tos(b, s.len)
} }
pub fn (s string) is_upper() bool {
for i in 0..s.len {
if s[i] >= `a` && s[i] <= `z` {
return false
}
}
return true
}
pub fn (s string) capitalize() string { pub fn (s string) capitalize() string {
if s.len == 0 { if s.len == 0 {
return '' return ''
@ -761,6 +779,18 @@ pub fn (s string) capitalize() string {
return cap return cap
} }
pub fn (s string) is_capital() bool {
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
return false
}
for i in 1..s.len {
if s[i] >= `A` && s[i] <= `Z` {
return false
}
}
return true
}
pub fn (s string) title() string { pub fn (s string) title() string {
words := s.split(' ') words := s.split(' ')
mut tit := []string mut tit := []string
@ -771,6 +801,16 @@ pub fn (s string) title() string {
return title return title
} }
pub fn (s string) is_title() bool {
words := s.split(' ')
for word in words {
if !word.is_capital() {
return false
}
}
return true
}
// 'hey [man] how you doin' // 'hey [man] how you doin'
// find_between('[', ']') == 'man' // find_between('[', ']') == 'man'
pub fn (s string) find_between(start, end string) string { pub fn (s string) find_between(start, end string) string {

View File

@ -331,38 +331,6 @@ fn test_runes() {
assert last.len == 2 assert last.len == 2
} }
fn test_lower() {
mut s := 'A'
assert s.to_lower() == 'a'
assert s.to_lower().len == 1
s = 'HELLO'
assert s.to_lower() == 'hello'
assert s.to_lower().len == 5
s = 'Aloha'
assert s.to_lower() == 'aloha'
s = 'Have A nice Day!'
assert s.to_lower() == 'have a nice day!'
s = 'hi'
assert s.to_lower() == 'hi'
assert 'aloha!'[0] == `a`
assert 'aloha!'[5] == `!`
}
fn test_upper() {
mut s := 'a'
assert s.to_upper() == 'A'
assert s.to_upper().len == 1
s = 'hello'
assert s.to_upper() == 'HELLO'
assert s.to_upper().len == 5
s = 'Aloha'
assert s.to_upper() == 'ALOHA'
s = 'have a nice day!'
assert s.to_upper() == 'HAVE A NICE DAY!'
s = 'hi'
assert s.to_upper() == 'HI'
}
fn test_left_right() { fn test_left_right() {
s := 'ALOHA' s := 'ALOHA'
assert s.left(3) == 'ALO' assert s.left(3) == 'ALO'
@ -520,23 +488,78 @@ fn test_count() {
assert 'bbaabb'.count('aa') == 1 assert 'bbaabb'.count('aa') == 1
} }
fn test_lower() {
mut s := 'A'
assert !s.is_lower()
assert s.to_lower() == 'a'
assert s.to_lower().len == 1
s = 'HELLO'
assert !s.is_lower()
assert s.to_lower() == 'hello'
assert s.to_lower().len == 5
s = 'Aloha'
assert !s.is_lower()
assert s.to_lower() == 'aloha'
s = 'Have A nice Day!'
assert !s.is_lower()
assert s.to_lower() == 'have a nice day!'
s = 'hi'
assert s.is_lower()
assert s.to_lower() == 'hi'
assert 'aloha!'[0] == `a`
assert 'aloha!'[5] == `!`
}
fn test_upper() {
mut s := 'a'
assert !s.is_upper()
assert s.to_upper() == 'A'
assert s.to_upper().len == 1
s = 'hello'
assert !s.is_upper()
assert s.to_upper() == 'HELLO'
assert s.to_upper().len == 5
s = 'Aloha'
assert !s.is_upper()
assert s.to_upper() == 'ALOHA'
s = 'have a nice day!'
assert !s.is_upper()
assert s.to_upper() == 'HAVE A NICE DAY!'
s = 'HI'
assert s.is_upper()
assert s.to_upper() == 'HI'
}
fn test_capitalize() { fn test_capitalize() {
mut s := 'hello' mut s := 'hello'
assert !s.is_capital()
assert s.capitalize() == 'Hello' assert s.capitalize() == 'Hello'
s = 'test' s = 'test'
assert !s.is_capital()
assert s.capitalize() == 'Test' assert s.capitalize() == 'Test'
s = 'i am ray' s = 'i am ray'
assert !s.is_capital()
assert s.capitalize() == 'I am ray' assert s.capitalize() == 'I am ray'
s = '' s = ''
assert !s.is_capital()
assert s.capitalize() == '' assert s.capitalize() == ''
s = 'TEST IT'
assert !s.is_capital()
assert s.capitalize() == 'Test it'
s = 'Test it'
assert s.is_capital()
assert s.capitalize() == 'Test it'
} }
fn test_title() { fn test_title() {
s := 'hello world' mut s := 'hello world'
assert !s.is_title()
assert s.title() == 'Hello World' assert s.title() == 'Hello World'
s.to_upper() s = 'HELLO WORLD'
assert !s.is_title()
assert s.title() == 'Hello World' assert s.title() == 'Hello World'
s.to_lower() s = 'Hello World'
assert s.is_title()
assert s.title() == 'Hello World' assert s.title() == 'Hello World'
} }

View File

@ -1821,8 +1821,8 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
if p.tok.kind == .key_else { if p.tok.kind == .key_else {
have_final_else = true have_final_else = true
p.next() p.next()
} else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() || } else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names ||
p.peek_tok.kind == .dot) { (p.tok.lit[0].is_capital() && !p.tok.lit.is_upper()) || p.peek_tok.kind == .dot) {
// Sum type match // Sum type match
// if sym.kind == .sum_type { // if sym.kind == .sum_type {
// p.warn('is sum') // p.warn('is sum')