builtin: implement []int.str()

pull/3994/head
Delyan Angelov 2020-03-11 19:48:59 +02:00 committed by GitHub
parent dff17ee0d7
commit de7fed45dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -353,7 +353,7 @@ pub fn (a array) free() {
} }
// []string.str returns a string representation of the array of strings // []string.str returns a string representation of the array of strings
// "[ 'a', 'b', 'c' ]" // => '["a", "b", "c"]'
pub fn (a []string) str() string { pub fn (a []string) str() string {
mut sb := strings.new_builder(a.len * 3) mut sb := strings.new_builder(a.len * 3)
sb.write('[') sb.write('[')
@ -370,8 +370,23 @@ pub fn (a []string) str() string {
return sb.str() return sb.str()
} }
// []int.str returns a string representation of the array of ints
// => '[1, 2, 3]'
pub fn (a []int) str() string {
mut sb := strings.new_builder(a.len * 13)
sb.write('[')
for i in 0..a.len {
sb.write(a[i].str())
if i < a.len - 1 {
sb.write(', ')
}
}
sb.write(']')
return sb.str()
}
// []bool.str returns a string representation of the array of bools // []bool.str returns a string representation of the array of bools
// "[true, true, false]" // => '[true, true, false]'
pub fn (a []bool) str() string { pub fn (a []bool) str() string {
mut sb := strings.new_builder(a.len * 3) mut sb := strings.new_builder(a.len * 3)
sb.write('[') sb.write('[')