Skip to content

Commit f02b423

Browse files
authored
builtin: string test refactor, string functions inlining (#17598)
1 parent ddcc22f commit f02b423

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

vlib/builtin/string.v

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub fn (s string) len_utf8() int {
308308

309309
// clone_static returns an independent copy of a given array.
310310
// It should be used only in -autofree generated code.
311+
[inline]
311312
fn (a string) clone_static() string {
312313
return a.clone()
313314
}
@@ -565,56 +566,67 @@ pub fn (s string) normalize_tabs(tab_len int) string {
565566
}
566567

567568
// bool returns `true` if the string equals the word "true" it will return `false` otherwise.
569+
[inline]
568570
pub fn (s string) bool() bool {
569571
return s == 'true' || s == 't' // TODO t for pg, remove
570572
}
571573

572574
// int returns the value of the string as an integer `'1'.int() == 1`.
575+
[inline]
573576
pub fn (s string) int() int {
574577
return int(strconv.common_parse_int(s, 0, 32, false, false) or { 0 })
575578
}
576579

577580
// i64 returns the value of the string as i64 `'1'.i64() == i64(1)`.
581+
[inline]
578582
pub fn (s string) i64() i64 {
579583
return strconv.common_parse_int(s, 0, 64, false, false) or { 0 }
580584
}
581585

582586
// i8 returns the value of the string as i8 `'1'.i8() == i8(1)`.
587+
[inline]
583588
pub fn (s string) i8() i8 {
584589
return i8(strconv.common_parse_int(s, 0, 8, false, false) or { 0 })
585590
}
586591

587592
// i16 returns the value of the string as i16 `'1'.i16() == i16(1)`.
593+
[inline]
588594
pub fn (s string) i16() i16 {
589595
return i16(strconv.common_parse_int(s, 0, 16, false, false) or { 0 })
590596
}
591597

592598
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
599+
[inline]
593600
pub fn (s string) f32() f32 {
594601
return f32(strconv.atof64(s) or { 0 })
595602
}
596603

597604
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
605+
[inline]
598606
pub fn (s string) f64() f64 {
599607
return strconv.atof64(s) or { 0 }
600608
}
601609

602610
// u8 returns the value of the string as u8 `'1'.u8() == u8(1)`.
611+
[inline]
603612
pub fn (s string) u8() u8 {
604613
return u8(strconv.common_parse_uint(s, 0, 8, false, false) or { 0 })
605614
}
606615

607616
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
617+
[inline]
608618
pub fn (s string) u16() u16 {
609619
return u16(strconv.common_parse_uint(s, 0, 16, false, false) or { 0 })
610620
}
611621

612622
// u32 returns the value of the string as u32 `'1'.u32() == u32(1)`.
623+
[inline]
613624
pub fn (s string) u32() u32 {
614625
return u32(strconv.common_parse_uint(s, 0, 32, false, false) or { 0 })
615626
}
616627

617628
// u64 returns the value of the string as u64 `'1'.u64() == u64(1)`.
629+
[inline]
618630
pub fn (s string) u64() u64 {
619631
return strconv.common_parse_uint(s, 0, 64, false, false) or { 0 }
620632
}
@@ -624,6 +636,7 @@ pub fn (s string) u64() u64 {
624636
// This method directly exposes the `parse_int` function from `strconv`
625637
// as a method on `string`. For more advanced features,
626638
// consider calling `strconv.common_parse_int` directly.
639+
[inline]
627640
pub fn (s string) parse_uint(_base int, _bit_size int) !u64 {
628641
return strconv.parse_uint(s, _base, _bit_size)
629642
}
@@ -644,6 +657,7 @@ pub fn (s string) parse_uint(_base int, _bit_size int) !u64 {
644657
// This method directly exposes the `parse_uint` function from `strconv`
645658
// as a method on `string`. For more advanced features,
646659
// consider calling `strconv.common_parse_uint` directly.
660+
[inline]
647661
pub fn (s string) parse_int(_base int, _bit_size int) !i64 {
648662
return strconv.parse_int(s, _base, _bit_size)
649663
}
@@ -791,6 +805,7 @@ pub fn (s string) rsplit_any(delim string) []string {
791805
// Example: assert 'A B C'.split(' ') == ['A','B','C']
792806
// If `delim` is empty the string is split by it's characters.
793807
// Example: assert 'DEF'.split('') == ['D','E','F']
808+
[inline]
794809
pub fn (s string) split(delim string) []string {
795810
return s.split_nth(delim, 0)
796811
}
@@ -799,6 +814,7 @@ pub fn (s string) split(delim string) []string {
799814
// Example: assert 'A B C'.rsplit(' ') == ['C','B','A']
800815
// If `delim` is empty the string is split by it's characters.
801816
// Example: assert 'DEF'.rsplit('') == ['F','E','D']
817+
[inline]
802818
pub fn (s string) rsplit(delim string) []string {
803819
return s.rsplit_nth(delim, 0)
804820
}
@@ -1018,6 +1034,7 @@ pub fn (s string) split_into_lines() []string {
10181034
}
10191035

10201036
// used internally for [2..4]
1037+
[inline]
10211038
fn (s string) substr2(start int, _end int, end_max bool) string {
10221039
end := if end_max { s.len } else { _end }
10231040
return s.substr(start, end)
@@ -1561,6 +1578,7 @@ pub fn (s string) find_between(start string, end string) string {
15611578

15621579
// trim_space strips any of ` `, `\n`, `\t`, `\v`, `\f`, `\r` from the start and end of the string.
15631580
// Example: assert ' Hello V '.trim_space() == 'Hello V'
1581+
[inline]
15641582
pub fn (s string) trim_space() string {
15651583
return s.trim(' \n\t\v\f\r')
15661584
}
@@ -1719,16 +1737,19 @@ fn compare_lower_strings(a &string, b &string) int {
17191737
}
17201738

17211739
// sort_ignore_case sorts the string array using case insesitive comparing.
1740+
[inline]
17221741
pub fn (mut s []string) sort_ignore_case() {
17231742
s.sort_with_compare(compare_lower_strings)
17241743
}
17251744

17261745
// sort_by_len sorts the the string array by each string's `.len` length.
1746+
[inline]
17271747
pub fn (mut s []string) sort_by_len() {
17281748
s.sort_with_compare(compare_strings_by_len)
17291749
}
17301750

17311751
// str returns a copy of the string
1752+
[inline]
17321753
pub fn (s string) str() string {
17331754
return s.clone()
17341755
}
@@ -1914,6 +1935,7 @@ pub fn (s string) all_after_first(sub string) string {
19141935
// Example: assert '23:34:45.234'.after(':') == '45.234'
19151936
// Example: assert 'abcd'.after('z') == 'abcd'
19161937
// TODO: deprecate either .all_after_last or .after
1938+
[inline]
19171939
pub fn (s string) after(sub string) string {
19181940
return s.all_after_last(sub)
19191941
}
@@ -1973,6 +1995,7 @@ pub fn (a []string) join(sep string) string {
19731995
}
19741996

19751997
// join joins a string array into a string using a `\n` newline delimiter.
1998+
[inline]
19761999
pub fn (s []string) join_lines() string {
19772000
return s.join('\n')
19782001
}
@@ -2107,6 +2130,7 @@ pub fn (s string) fields() []string {
21072130
// this is a string,
21082131
// Everything before the first | is removed'
21092132
// ```
2133+
[inline]
21102134
pub fn (s string) strip_margin() string {
21112135
return s.strip_margin_custom(`|`)
21122136
}
@@ -2360,6 +2384,7 @@ pub fn (name string) match_glob(pattern string) bool {
23602384
}
23612385

23622386
// is_ascii returns true if all characters belong to the US-ASCII set ([` `..`~`])
2387+
[inline]
23632388
pub fn (s string) is_ascii() bool {
23642389
return !s.bytes().any(it < u8(` `) || it > u8(`~`))
23652390
}

vlib/builtin/string_test.v

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ fn test_split_nth() {
172172
assert b.split_nth('::', 2).len == 2
173173
assert b.split_nth('::', 10).len == 3
174174
c := 'ABCDEF'
175-
println(c.split('').len)
176175
assert c.split('').len == 6
177176
assert c.split_nth('', 3).len == 3
178177
assert c.split_nth('BC', -1).len == 2
@@ -760,7 +759,6 @@ fn test_bytes_to_string() {
760759

761760
fn test_charptr() {
762761
foo := &char('VLANG'.str)
763-
println(typeof(foo).name)
764762
assert typeof(foo).name == '&char'
765763
assert unsafe { foo.vstring() } == 'VLANG'
766764
assert unsafe { foo.vstring_with_len(3) } == 'VLA'
@@ -872,7 +870,6 @@ fn test_for_loop_two() {
872870

873871
fn test_quote() {
874872
a := `'`
875-
println('testing double quotes')
876873
b := 'hi'
877874
assert b == 'hi'
878875
assert a.str() == "'"
@@ -946,9 +943,7 @@ fn test_trim_string_right() {
946943
fn test_raw() {
947944
raw := r'raw\nstring'
948945
lines := raw.split('\n')
949-
println(lines)
950946
assert lines.len == 1
951-
println('raw string: "${raw}"')
952947

953948
raw2 := r'Hello V\0'
954949
assert raw2[7] == `\\`
@@ -970,7 +965,6 @@ fn test_raw_with_quotes() {
970965

971966
fn test_escape() {
972967
a := 10
973-
println("\"${a}")
974968
assert "\"${a}" == '"10'
975969
}
976970

@@ -986,7 +980,6 @@ fn test_atoi() {
986980

987981
fn test_raw_inter() {
988982
world := 'world'
989-
println(world)
990983
s := r'hello\n$world'
991984
assert s == r'hello\n$world'
992985
assert s.contains('$')
@@ -995,24 +988,22 @@ fn test_raw_inter() {
995988
fn test_c_r() {
996989
// This used to break because of r'' and c''
997990
c := 42
998-
println('${c}')
991+
cs := '${c}'
999992
r := 50
1000-
println('${r}')
993+
rs := '${r}'
1001994
}
1002995

1003996
fn test_inter_before_comptime_if() {
1004997
s := '123'
1005998
// This used to break ('123 $....')
1006999
$if linux {
1007-
println(s)
10081000
}
10091001
assert s == '123'
10101002
}
10111003

10121004
fn test_double_quote_inter() {
10131005
a := 1
10141006
b := 2
1015-
println('${a} ${b}')
10161007
assert '${a} ${b}' == '1 2'
10171008
assert '${a} ${b}' == '1 2'
10181009
}

0 commit comments

Comments
 (0)