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

cache string.hash()

This commit is contained in:
Alexander Medvednikov 2019-07-10 09:48:10 +02:00
parent 2ab1ad8b30
commit 28843a12c7
7 changed files with 15 additions and 21 deletions

View File

@ -49,11 +49,6 @@ enum Pass {
main
}
fn mykek(o OS) {
}
struct V {
mut:
os OS // the OS to build for

View File

@ -2301,7 +2301,7 @@ fn (p mut Parser) array_init() string {
name := p.check_name()
if p.table.known_type(name) {
p.cgen.cur_line = ''
p.gen('{} /* arkek init*/')
p.gen('{}')
return '[$lit]$name'
}
else {

View File

@ -314,8 +314,8 @@ fn (s mut Scanner) scan() ScanRes {
case `$`:
return scan_res(.dollar, '')
case `}`:
// s = `hello $name kek`
// s = `hello ${name} kek`
// s = `hello $name !`
// s = `hello ${name} !`
if s.inside_string {
s.pos++
// TODO UN.neEDED?

View File

@ -32,7 +32,7 @@ mut:
fields []Var
methods []Fn
parent string
func Fn // For cat == FN (type kek fn())
func Fn // For cat == FN (type myfn fn())
is_c bool // C.FI.le
is_interface bool
is_enum bool

View File

@ -6,6 +6,8 @@ module builtin
// V strings are not null-terminated.
struct string {
mut:
hash_cache int
pub:
str byteptr
len int
@ -797,13 +799,12 @@ pub fn (c byte) is_white() bool {
pub fn (s string) hash() int {
mut hash := int(0)
for i := 0; i < s.len; i++ {
// if key == 'Content-Type' {
// println('$i) $hash')
// }
hash = hash * int(31) + int(s.str[i])
}
return hash
mut h := s.hash_cache
if h == 0 && s.len > 0 {
for c in s {
h = h * 31 + int(c)
}
}
return h
}

View File

@ -155,12 +155,12 @@ fn test_replace() {
assert b==('hello world')
b = b.replace('h', 'H')
assert b==('Hello world')
b = b.replace('kek', 'lul')
b = b.replace('foo', 'bar')
assert b==('Hello world')
s := 'hey man how are you'
assert s.replace('man ', '') == 'hey how are you'
lol := 'lol lol lol'
assert lol.replace('lol', 'kek') == 'kek kek kek'
assert lol.replace('lol', 'LOL') == 'LOL LOL LOL'
b = 'oneBtwoBBthree'
assert b.replace('B', '') == 'onetwothree'
b = '**char'

View File

@ -54,7 +54,6 @@ pub fn init() {
struct Face {
cobj voidptr
kek int
}
struct Cfg {
@ -368,7 +367,6 @@ pub fn new_context_text(cfg Cfg, scale int) *GG {
mut chars := []gg.Character{}
f := Face {
cobj: 0
kek: 0
}
# f.cobj = &face;
// # for (GLubyte c = 0; c < 128; c++)