builtin: fix byte.str() (part 1)
parent
fada097b47
commit
3e04dfc79f
|
@ -452,6 +452,19 @@ pub fn (b byte) str() string {
|
||||||
return str
|
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`.
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
||||||
// Example: assert byte(0).str_escaped() == r'`\0`'
|
// Example: assert byte(0).str_escaped() == r'`\0`'
|
||||||
pub fn (b byte) str_escaped() string {
|
pub fn (b byte) str_escaped() string {
|
||||||
|
@ -464,7 +477,7 @@ pub fn (b byte) str_escaped() string {
|
||||||
11 { r'`\v`' }
|
11 { r'`\v`' }
|
||||||
12 { r'`\f`' }
|
12 { r'`\f`' }
|
||||||
13 { r'`\r`' }
|
13 { r'`\r`' }
|
||||||
32...126 { b.str() }
|
32...126 { b.ascii_str() }
|
||||||
else { '0x' + b.hex() }
|
else { '0x' + b.hex() }
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
|
|
|
@ -529,7 +529,7 @@ pub fn (s string) split_nth(delim string, nth int) []string {
|
||||||
res << s.right(i)
|
res << s.right(i)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
res << ch.str()
|
res << ch.ascii_str()
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
return res
|
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.
|
// 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 {
|
pub fn (s string) index_any(chars string) int {
|
||||||
for c in chars {
|
for c in chars {
|
||||||
index := s.index(c.str()) or { continue }
|
index := s.index(c.ascii_str()) or { continue }
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
return -1
|
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`.
|
// contains_any returns `true` if the string contains any chars in `chars`.
|
||||||
pub fn (s string) contains_any(chars string) bool {
|
pub fn (s string) contains_any(chars string) bool {
|
||||||
for c in chars {
|
for c in chars {
|
||||||
if c.str() in s {
|
if c.ascii_str() in s {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -923,7 +923,7 @@ pub fn (s string) capitalize() string {
|
||||||
if s.len == 0 {
|
if s.len == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return s[0].str().to_upper() + s[1..]
|
return s[0].ascii_str().to_upper() + s[1..]
|
||||||
// sl := s.to_lower()
|
// sl := s.to_lower()
|
||||||
// cap := sl[0].str().to_upper() + sl.right(1)
|
// cap := sl[0].str().to_upper() + sl.right(1)
|
||||||
// return cap
|
// return cap
|
||||||
|
|
|
@ -383,7 +383,7 @@ pub fn (fs FlagParser) usage() string {
|
||||||
for f in fs.flags {
|
for f in fs.flags {
|
||||||
mut onames := []string{}
|
mut onames := []string{}
|
||||||
if f.abbr != 0 {
|
if f.abbr != 0 {
|
||||||
onames << '-$f.abbr.str()'
|
onames << '-$f.abbr.ascii_str()'
|
||||||
}
|
}
|
||||||
if f.name != '' {
|
if f.name != '' {
|
||||||
if !f.val_desc.contains('<bool>') {
|
if !f.val_desc.contains('<bool>') {
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub fn format_int(n i64, radix int) string {
|
||||||
}
|
}
|
||||||
mut res := ''
|
mut res := ''
|
||||||
for n_copy != 0 {
|
for n_copy != 0 {
|
||||||
res = base_digits[n_copy % radix].str() + res
|
res = base_digits[n_copy % radix].ascii_str() + res
|
||||||
n_copy /= radix
|
n_copy /= radix
|
||||||
}
|
}
|
||||||
return '$sign$res'
|
return '$sign$res'
|
||||||
|
@ -38,7 +38,7 @@ pub fn format_uint(n u64, radix int) string {
|
||||||
mut res := ''
|
mut res := ''
|
||||||
uradix := u64(radix)
|
uradix := u64(radix)
|
||||||
for n_copy != 0 {
|
for n_copy != 0 {
|
||||||
res = base_digits[n_copy % uradix].str() + res
|
res = base_digits[n_copy % uradix].ascii_str() + res
|
||||||
n_copy /= uradix
|
n_copy /= uradix
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
|
|
@ -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)
|
// 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
|
// TODO: remove this for actual methods, use only for compiler magic
|
||||||
// FIXME: Argument count != 1 will break these
|
// FIXME: Argument count != 1 will break these
|
||||||
if left_type_sym.kind == .array && method_name in array_builtin_methods {
|
if left_type_sym.kind == .array && method_name in array_builtin_methods {
|
||||||
|
|
|
@ -973,7 +973,7 @@ fn (mut s Scanner) text_scan() token.Token {
|
||||||
return s.end_of_file()
|
return s.end_of_file()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.error('invalid character `$c.str()`')
|
s.error('invalid character `$c.ascii_str()`')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return s.end_of_file()
|
return s.end_of_file()
|
||||||
|
|
|
@ -131,7 +131,7 @@ pub fn source_context(kind string, source string, column int, pos token.Position
|
||||||
mut pointerline := ''
|
mut pointerline := ''
|
||||||
for bchar in sline[..start_column] {
|
for bchar in sline[..start_column] {
|
||||||
x := if bchar.is_space() { bchar } else { ` ` }
|
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 { '^' }
|
underline := if pos.len > 1 { '~'.repeat(end_column - start_column) } else { '^' }
|
||||||
pointerline += bold(color(kind, underline))
|
pointerline += bold(color(kind, underline))
|
||||||
|
|
|
@ -28,14 +28,14 @@ pub fn smart_quote(str string, raw bool) string {
|
||||||
}
|
}
|
||||||
if pos + 1 < len {
|
if pos + 1 < len {
|
||||||
unsafe {
|
unsafe {
|
||||||
next = str.str[pos + 1].str()
|
next = str.str[pos + 1].ascii_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mut current := str
|
mut current := str
|
||||||
mut toadd := str
|
mut toadd := str
|
||||||
if len > 1 {
|
if len > 1 {
|
||||||
unsafe {
|
unsafe {
|
||||||
current = str.str[pos].str()
|
current = str.str[pos].ascii_str()
|
||||||
}
|
}
|
||||||
toadd = current
|
toadd = current
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ fn (mut s Scanner) create_string(q byte) string {
|
||||||
str += s.text[s.pos..s.pos + 1]
|
str += s.text[s.pos..s.pos + 1]
|
||||||
s.pos += 2
|
s.pos += 2
|
||||||
} else {
|
} else {
|
||||||
str += s.text[s.pos].str()
|
str += s.text[s.pos].ascii_str()
|
||||||
s.pos++
|
s.pos++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,7 @@ fn (mut s Scanner) create_string(q byte) string {
|
||||||
fn (mut s Scanner) create_ident() string {
|
fn (mut s Scanner) create_ident() string {
|
||||||
mut text := ''
|
mut text := ''
|
||||||
for is_name_alpha(s.text[s.pos]) {
|
for is_name_alpha(s.text[s.pos]) {
|
||||||
text += s.text[s.pos].str()
|
text += s.text[s.pos].ascii_str()
|
||||||
s.pos++
|
s.pos++
|
||||||
}
|
}
|
||||||
return text
|
return text
|
||||||
|
@ -138,13 +138,13 @@ fn (mut s Scanner) scan_all() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
match c {
|
match c {
|
||||||
`{` { s.tokenize(.lcbr, c.str()) }
|
`{` { s.tokenize(.lcbr, c.ascii_str()) }
|
||||||
`}` { s.tokenize(.rcbr, c.str()) }
|
`}` { s.tokenize(.rcbr, c.ascii_str()) }
|
||||||
`[` { s.tokenize(.labr, c.str()) }
|
`[` { s.tokenize(.labr, c.ascii_str()) }
|
||||||
`]` { s.tokenize(.rabr, c.str()) }
|
`]` { s.tokenize(.rabr, c.ascii_str()) }
|
||||||
`:` { s.tokenize(.colon, c.str()) }
|
`:` { s.tokenize(.colon, c.ascii_str()) }
|
||||||
`,` { s.tokenize(.comma, c.str()) }
|
`,` { s.tokenize(.comma, c.ascii_str()) }
|
||||||
else { s.tokenize(.unknown, c.str()) }
|
else { s.tokenize(.unknown, c.ascii_str()) }
|
||||||
}
|
}
|
||||||
s.pos++
|
s.pos++
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue