From 0ced7116b6956de7b4db77dd9f4d1974fb42329c Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 12 Apr 2020 18:09:05 +0800 Subject: [PATCH] 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 --- vlib/builtin/string.v | 40 ++++++++++++++++ vlib/builtin/string_test.v | 93 ++++++++++++++++++++++++-------------- vlib/v/parser/parser.v | 4 +- 3 files changed, 100 insertions(+), 37 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 8469696739..720aa22f33 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -744,6 +744,15 @@ pub fn (s string) to_lower() string { 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 { mut b := malloc(s.len + 1) for i in 0..s.len { @@ -752,6 +761,15 @@ pub fn (s string) to_upper() string { 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 { if s.len == 0 { return '' @@ -761,6 +779,18 @@ pub fn (s string) capitalize() string { 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 { words := s.split(' ') mut tit := []string @@ -771,6 +801,16 @@ pub fn (s string) title() string { 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' // find_between('[', ']') == 'man' pub fn (s string) find_between(start, end string) string { diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 901fbff681..6688664387 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -331,38 +331,6 @@ fn test_runes() { 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() { s := 'ALOHA' assert s.left(3) == 'ALO' @@ -520,23 +488,78 @@ fn test_count() { 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() { mut s := 'hello' + assert !s.is_capital() assert s.capitalize() == 'Hello' s = 'test' + assert !s.is_capital() assert s.capitalize() == 'Test' s = 'i am ray' + assert !s.is_capital() assert s.capitalize() == 'I am ray' s = '' + assert !s.is_capital() 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() { - s := 'hello world' + mut s := 'hello world' + assert !s.is_title() assert s.title() == 'Hello World' - s.to_upper() + s = 'HELLO WORLD' + assert !s.is_title() assert s.title() == 'Hello World' - s.to_lower() + s = 'Hello World' + assert s.is_title() assert s.title() == 'Hello World' } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index aa1e030757..e8bf91bf04 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1821,8 +1821,8 @@ fn (p mut Parser) match_expr() ast.MatchExpr { if p.tok.kind == .key_else { have_final_else = true p.next() - } else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() || - p.peek_tok.kind == .dot) { + } else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names || + (p.tok.lit[0].is_capital() && !p.tok.lit.is_upper()) || p.peek_tok.kind == .dot) { // Sum type match // if sym.kind == .sum_type { // p.warn('is sum')