forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ts
More file actions
114 lines (107 loc) · 2.7 KB
/
string.ts
File metadata and controls
114 lines (107 loc) · 2.7 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function isSpace(s: string) {
return (
" \t\n\r\u000B\u000C\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff".indexOf(
s
) >= 0
)
}
String.prototype.trim = function (this: string) {
let beg = 0
while (beg < this.length) {
if (!isSpace(this[beg])) break
beg++
}
let end = this.length - 1
while (end >= beg) {
if (!isSpace(this[end])) break
end--
}
return this.slice(beg, end + 1)
}
String.prototype.lastIndexOf = function (
this: string,
searchString: string,
position?: number
): number {
if (position === undefined) position = this.length
return this.indexOf(searchString, 0, -position)
}
String.prototype.includes = function (
this: string,
searchString: string,
position?: number
): boolean {
return this.indexOf(searchString, position) >= 0
}
String.prototype.endsWith = function (
this: string,
searchString: string,
endPosition?: number
): boolean {
if (!(endPosition < this.length)) endPosition = this.length
return (
this.indexOf(
searchString,
endPosition - searchString.length,
endPosition
) >= 0
)
}
String.prototype.startsWith = function (
this: string,
searchString: string,
position?: number
): boolean {
if (!(position > 0)) position = 0
return this.indexOf(searchString, position, position + 1) >= 0
}
String.prototype.split = function (
this: string,
separator?: string,
limit?: number
): string[] {
const S = this
// https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.split
const A: string[] = []
let lim = 0
if (limit === undefined) lim = (1 << 29) - 1
// spec says 1 << 53, leaving it at 29 for constant folding
else if (limit < 0) lim = 0
else lim = limit | 0
const s = S.length
let p = 0
const R = separator
if (lim === 0) return A
if (separator === undefined) {
A[0] = S
return A
}
if (s === 0) {
let z = splitMatch(S, 0, R)
if (z > -1) return A
A[0] = S
return A
}
let T: string
let q = p
while (q !== s) {
let e = splitMatch(S, q, R)
if (e < 0) q++
else {
if (e === p) q++
else {
T = S.slice(p, q)
A.push(T)
if (A.length === lim) return A
p = e
q = p
}
}
}
T = S.slice(p, q)
A.push(T)
return A
}
function splitMatch(S: string, q: number, R: string): number {
return S.indexOf(R, q, q + 1) === q ? q + R.length : -1
}