1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: rune type for `` literals

This commit is contained in:
Alexander Medvednikov
2020-08-27 06:46:18 +02:00
parent 99dd72efea
commit 6921d46185
17 changed files with 173 additions and 108 deletions

View File

@ -607,6 +607,15 @@ pub fn (a []byte) index(v byte) int {
return -1
}
pub fn (a []rune) index(v rune) int {
for i in 0..a.len {
if a[i] == v {
return i
}
}
return -1
}
// []char.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
// TODO is `char` type yet in the language?

View File

@ -1,8 +1,19 @@
fn test_clone() {
a := [byte(0), 1, 2]
a := [byte(0), 1, 2]
b := a.clone()
assert b.len == 3
assert b[0] == 0
assert b[1] == 1
assert b[2] == 2
}
assert b[0] == 0
assert b[1] == 1
assert b[2] == 2
println(b[1].str() )
println(typeof(`A`))
x := rune(`A`)
assert x.str() == 'A'
assert typeof(x) == 'rune'
//
y := `Z`
assert typeof(y) == 'rune'
assert y.str() == 'Z'
// assert b[1].str() == '1' TODO
}

View File

@ -368,80 +368,21 @@ pub fn (nn byteptr) str() string {
return u64(nn).hex()
}
// ----- utilities functions -----
/*
pub fn (c rune) str() string {
fst_byte := int(c)>>8 * 3 & 0xff
len := utf8_char_len(fst_byte)
mut str := string{
len: len
str: malloc(len + 1)
}
for i in 0..len {
str.str[i] = int(c)>>8 * (3 - i) & 0xff
}
str.str[len] = `\0`
return str
}
*/
pub fn (c byte) str() string {
pub fn (b byte) str() string {
// TODO
//return int(b).str_l(7)
mut str := string{
str: malloc(2)
len: 1
}
unsafe {
str.str[0] = c
str.str[0] = b
str.str[1] = `\0`
}
//println(str)
return str
}
/*
type rune = int
pub fn (r rune) str() string {
mut str := string{
str: malloc(2)
len: 1
}
unsafe {
str.str[0] = r
str.str[1] = `\0`
}
return str
}
*/
pub fn (c byte) is_capital() bool {
return c >= `A` && c <= `Z`
}
pub fn (b []byte) clone() []byte {
mut res := []byte{len: b.len}
//mut res := make([]byte, {repeat:b.len})
for i in 0..b.len {
res[i] = b[i]
}
return res
}
// TODO remove this once runes are implemented
pub fn (b []byte) bytestr() string {
return bytes2string(b)
}
// TODO copy pasted from builder.v
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << `\0`
res := tos(copy.data, copy.len-1)
return res
}
// TODO generic
pub fn (a []byte) contains(val byte) bool {
for aa in a {

54
vlib/builtin/rune.v Normal file
View File

@ -0,0 +1,54 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module builtin
type rune = int
pub fn (c rune) str() string {
return utf32_to_str(u32(c))
/*
unsafe {
fst_byte := int(c)>>8 * 3 & 0xff
len := utf8_char_len(byte(fst_byte))
println('len=$len')
mut str := string{
len: len
str: malloc(len + 1)
}
for i in 0..len {
str.str[i] = byte(int(c)>>8 * (3 - i) & 0xff)
}
str.str[len] = `\0`
println(str)
return str
}
*/
}
// Define this on byte as well, so that we can do `s[0].is_capital()`
pub fn (c byte) is_capital() bool {
return c >= `A` && c <= `Z`
}
pub fn (b []byte) clone() []byte {
mut res := []byte{len: b.len}
//mut res := make([]byte, {repeat:b.len})
for i in 0..b.len {
res[i] = b[i]
}
return res
}
// TODO remove this once runes are implemented
pub fn (b []byte) bytestr() string {
return bytes2string(b)
}
// TODO copy pasted from builder.v
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << `\0`
res := tos(copy.data, copy.len-1)
return res
}

View File

@ -504,7 +504,7 @@ fn test_bytes_to_string() {
}
assert unsafe { buf.vstring() } == 'hello'
assert unsafe { buf.vstring_with_len(2) } == 'he'
bytes := [`h`, `e`, `l`, `l`, `o`]
bytes := [byte(`h`), `e`, `l`, `l`, `o`]
assert bytes.bytestr() == 'hello'
}