1.0.5 • Published 3 years ago
nv-string-cursor v1.0.5
nv-string-cursor
- nv-string-cursor
- avoid big str copy during tokenize
install
- npm install nv-string-cursor
usage
example
str cursor
const {Cache} = require("nv-string-cursor")
var cache = new Cache();
cache.push_pos(0,2);
cache.push_str("def\tghi");
cache.push_pos(12,15);
> cache
Cache(3) [
_Pos { type: 0, str: '', si: 0, ei: 2 },
_Str { type: 1, str: 'def\tghi', si: -1, ei: -1 },
_Pos { type: 0, str: '', si: 12, ei: 15 }
]
>
> cache.take(s)
'abdef\tghijkl'
>
> cache
Cache(0) []
>
var cache = new Cache();
cache.push_pos(0);
> cache
Cache(1) [ _Pos { type: 0, str: '', si: 0, ei: -1 } ]
cache.ei = 3
> cache
Cache(1) [ _Pos { type: 0, str: '', si: 0, ei: 3 } ]
>
cache.push_str("def\tghi");
cache.push_pos(12);
> cache
Cache(3) [
_Pos { type: 0, str: '', si: 0, ei: 3 },
_Str { type: 1, str: 'def\tghi', si: -1, ei: -1 },
_Pos { type: 0, str: '', si: 12, ei: -1 }
]
>
cache.ei=15
> cache.take(s)
'abcdef\tghijkl'
char cursor
var s = "~🚢~"
var cursor = new Cursor()
/*Cursor { prev: -1, curr: 0 }*/
> cursor.forward(s)
1
> cursor
Cursor { prev: 0, curr: 1 }
>
> cursor.forward(s)
3
> cursor
Cursor { prev: 1, curr: 3 }
>
> cursor.ch(s)
'🚢'
>
> cursor.forward(s)
4
> cursor
Cursor { prev: 3, curr: 4 }
>
> cursor.forward(s)
undefined
> cursor.ch(s)
''
> cursor
Cursor { prev: 4, curr: undefined }
>
> cursor.is_eof()
true
>
METHODS
Cache
cache.ei
cache.lst
cache.push_pos
cache.push_str
cache.take
cache.clear
Cursor
cursor.back //only once ,and after back cant take ch
cursor.ch
cursor.forward
cursor.is_eof
cursor.curr
cursor.prev
LICENSE
- ISC