builtin: fix byte.str() (part 1)

pull/7902/head
Alexander Medvednikov 2021-01-05 18:59:51 +01:00
parent fada097b47
commit 3e04dfc79f
9 changed files with 67 additions and 49 deletions

View File

@ -452,6 +452,19 @@ pub fn (b byte) str() string {
return str
}
pub fn (b byte) ascii_str() string {
mut str := string{
str: malloc(2)
len: 1
}
unsafe {
str.str[0] = b
str.str[1] = `\0`
}
// println(str)
return str
}
// str_escaped returns the contents of `byte` as an escaped `string`.
// Example: assert byte(0).str_escaped() == r'`\0`'
pub fn (b byte) str_escaped() string {
@ -464,7 +477,7 @@ pub fn (b byte) str_escaped() string {
11 { r'`\v`' }
12 { r'`\f`' }
13 { r'`\r`' }
32...126 { b.str() }
32...126 { b.ascii_str() }
else { '0x' + b.hex() }
}
return str

View File

@ -529,7 +529,7 @@ pub fn (s string) split_nth(delim string, nth int) []string {
res << s.right(i)
break
}
res << ch.str()
res << ch.ascii_str()
i++
}
return res
@ -716,7 +716,7 @@ fn (s string) index_kmp(p string) int {
// index_any returns the position of any of the characters in the input string - if found.
pub fn (s string) index_any(chars string) int {
for c in chars {
index := s.index(c.str()) or { continue }
index := s.index(c.ascii_str()) or { continue }
return index
}
return -1
@ -825,7 +825,7 @@ pub fn (s string) contains(substr string) bool {
// contains_any returns `true` if the string contains any chars in `chars`.
pub fn (s string) contains_any(chars string) bool {
for c in chars {
if c.str() in s {
if c.ascii_str() in s {
return true
}
}
@ -923,7 +923,7 @@ pub fn (s string) capitalize() string {
if s.len == 0 {
return ''
}
return s[0].str().to_upper() + s[1..]
return s[0].ascii_str().to_upper() + s[1..]
// sl := s.to_lower()
// cap := sl[0].str().to_upper() + sl.right(1)
// return cap

View File

@ -383,7 +383,7 @@ pub fn (fs FlagParser) usage() string {
for f in fs.flags {
mut onames := []string{}
if f.abbr != 0 {
onames << '-$f.abbr.str()'
onames << '-$f.abbr.ascii_str()'
}
if f.name != '' {
if !f.val_desc.contains('<bool>') {

View File

@ -19,7 +19,7 @@ pub fn format_int(n i64, radix int) string {
}
mut res := ''
for n_copy != 0 {
res = base_digits[n_copy % radix].str() + res
res = base_digits[n_copy % radix].ascii_str() + res
n_copy /= radix
}
return '$sign$res'
@ -38,7 +38,7 @@ pub fn format_uint(n u64, radix int) string {
mut res := ''
uradix := u64(radix)
for n_copy != 0 {
res = base_digits[n_copy % uradix].str() + res
res = base_digits[n_copy % uradix].ascii_str() + res
n_copy /= uradix
}
return res

View File

@ -1222,6 +1222,11 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
}
// call_expr.generic_type = c.unwrap_generic(call_expr.generic_type)
}
/*
if left_type == table.byte_type && method_name == 'str' {
c.warn('byte str', call_expr.pos)
}
*/
// TODO: remove this for actual methods, use only for compiler magic
// FIXME: Argument count != 1 will break these
if left_type_sym.kind == .array && method_name in array_builtin_methods {

View File

@ -973,7 +973,7 @@ fn (mut s Scanner) text_scan() token.Token {
return s.end_of_file()
}
}
s.error('invalid character `$c.str()`')
s.error('invalid character `$c.ascii_str()`')
break
}
return s.end_of_file()

View File

@ -131,7 +131,7 @@ pub fn source_context(kind string, source string, column int, pos token.Position
mut pointerline := ''
for bchar in sline[..start_column] {
x := if bchar.is_space() { bchar } else { ` ` }
pointerline += x.str()
pointerline += x.ascii_str()
}
underline := if pos.len > 1 { '~'.repeat(end_column - start_column) } else { '^' }
pointerline += bold(color(kind, underline))

View File

@ -28,14 +28,14 @@ pub fn smart_quote(str string, raw bool) string {
}
if pos + 1 < len {
unsafe {
next = str.str[pos + 1].str()
next = str.str[pos + 1].ascii_str()
}
}
mut current := str
mut toadd := str
if len > 1 {
unsafe {
current = str.str[pos].str()
current = str.str[pos].ascii_str()
}
toadd = current
}

View File

@ -87,7 +87,7 @@ fn (mut s Scanner) create_string(q byte) string {
str += s.text[s.pos..s.pos + 1]
s.pos += 2
} else {
str += s.text[s.pos].str()
str += s.text[s.pos].ascii_str()
s.pos++
}
}
@ -97,7 +97,7 @@ fn (mut s Scanner) create_string(q byte) string {
fn (mut s Scanner) create_ident() string {
mut text := ''
for is_name_alpha(s.text[s.pos]) {
text += s.text[s.pos].str()
text += s.text[s.pos].ascii_str()
s.pos++
}
return text
@ -138,13 +138,13 @@ fn (mut s Scanner) scan_all() {
continue
}
match c {
`{` { s.tokenize(.lcbr, c.str()) }
`}` { s.tokenize(.rcbr, c.str()) }
`[` { s.tokenize(.labr, c.str()) }
`]` { s.tokenize(.rabr, c.str()) }
`:` { s.tokenize(.colon, c.str()) }
`,` { s.tokenize(.comma, c.str()) }
else { s.tokenize(.unknown, c.str()) }
`{` { s.tokenize(.lcbr, c.ascii_str()) }
`}` { s.tokenize(.rcbr, c.ascii_str()) }
`[` { s.tokenize(.labr, c.ascii_str()) }
`]` { s.tokenize(.rabr, c.ascii_str()) }
`:` { s.tokenize(.colon, c.ascii_str()) }
`,` { s.tokenize(.comma, c.ascii_str()) }
else { s.tokenize(.unknown, c.ascii_str()) }
}
s.pos++
}