/
seek.go
56 lines (51 loc) · 1.06 KB
/
seek.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright 2020, Vitali Baumtrok.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
package linescanner
func seekLineEnd(bytes []byte, from, to int) (int, int) {
for i := from; i < to; i++ {
if bytes[i] == '\n' {
return i, i + 1
} else if bytes[i] == '\r' {
if i+1 < to && bytes[i+1] == '\n' {
return i, i + 2
}
return i, i + 1
}
}
return to, to
}
func seekContent(bytes []byte, from, to int) int {
for i := from; i < to; i++ {
if bytes[i] < 0 || bytes[i] > 32 {
return i
}
}
return to
}
func seekContentRight(bytes []byte, from, to int) int {
for i := to - 1; i >= from; i-- {
if bytes[i] < 0 || bytes[i] > 32 {
return i + 1
}
}
return from
}
func seekBytes(bytes, word []byte, from, to int) int {
if len(word) > 0 {
limit := to - len(word) + 1
outter:
for i := from; i < limit; i++ {
for j, b := range word {
if bytes[i+j] != b {
continue outter
}
}
return i
}
}
return to
}