UNPKG

7.04 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17/*---------------------------------------------------------------------------------------------
18 * Copyright (c) Microsoft Corporation. All rights reserved.
19 * Licensed under the MIT License. See License.txt in the project root for license information.
20 *--------------------------------------------------------------------------------------------*/
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.compareSubstringIgnoreCase = exports.compareIgnoreCase = exports.compareSubstring = exports.compare = exports.unescapeInvisibleChars = exports.escapeInvisibleChars = exports.split = exports.startsWithIgnoreCase = exports.escapeRegExpCharacters = exports.commonPrefixLength = exports.equalsIgnoreCase = exports.isUpperAsciiLetter = exports.isLowerAsciiLetter = exports.endsWith = void 0;
23/**
24 * Determines if haystack ends with needle.
25 */
26function endsWith(haystack, needle) {
27 const diff = haystack.length - needle.length;
28 if (diff > 0) {
29 return haystack.indexOf(needle, diff) === diff;
30 }
31 else if (diff === 0) {
32 return haystack === needle;
33 }
34 else {
35 return false;
36 }
37}
38exports.endsWith = endsWith;
39function isLowerAsciiLetter(code) {
40 return code >= 97 /* a */ && code <= 122 /* z */;
41}
42exports.isLowerAsciiLetter = isLowerAsciiLetter;
43function isUpperAsciiLetter(code) {
44 return code >= 65 /* A */ && code <= 90 /* Z */;
45}
46exports.isUpperAsciiLetter = isUpperAsciiLetter;
47function isAsciiLetter(code) {
48 return isLowerAsciiLetter(code) || isUpperAsciiLetter(code);
49}
50function equalsIgnoreCase(a, b) {
51 const len1 = a ? a.length : 0;
52 const len2 = b ? b.length : 0;
53 if (len1 !== len2) {
54 return false;
55 }
56 return doEqualsIgnoreCase(a, b);
57}
58exports.equalsIgnoreCase = equalsIgnoreCase;
59function doEqualsIgnoreCase(a, b, stopAt = a.length) {
60 if (typeof a !== 'string' || typeof b !== 'string') {
61 return false;
62 }
63 for (let i = 0; i < stopAt; i++) {
64 const codeA = a.charCodeAt(i);
65 const codeB = b.charCodeAt(i);
66 if (codeA === codeB) {
67 continue;
68 }
69 // a-z A-Z
70 if (isAsciiLetter(codeA) && isAsciiLetter(codeB)) {
71 const diff = Math.abs(codeA - codeB);
72 if (diff !== 0 && diff !== 32) {
73 return false;
74 }
75 }
76 // Any other charcode
77 // tslint:disable-next-line:one-line
78 else {
79 if (String.fromCharCode(codeA).toLowerCase() !== String.fromCharCode(codeB).toLowerCase()) {
80 return false;
81 }
82 }
83 }
84 return true;
85}
86/**
87 * @returns the length of the common prefix of the two strings.
88 */
89function commonPrefixLength(a, b) {
90 let i;
91 const len = Math.min(a.length, b.length);
92 for (i = 0; i < len; i++) {
93 if (a.charCodeAt(i) !== b.charCodeAt(i)) {
94 return i;
95 }
96 }
97 return len;
98}
99exports.commonPrefixLength = commonPrefixLength;
100/**
101 * Escapes regular expression characters in a given string
102 */
103function escapeRegExpCharacters(value) {
104 return value.replace(/[\-\\\{\}\*\+\?\|\^\$\.\[\]\(\)\#]/g, '\\$&');
105}
106exports.escapeRegExpCharacters = escapeRegExpCharacters;
107function startsWithIgnoreCase(str, candidate) {
108 const candidateLength = candidate.length;
109 if (candidate.length > str.length) {
110 return false;
111 }
112 return doEqualsIgnoreCase(str, candidate, candidateLength);
113}
114exports.startsWithIgnoreCase = startsWithIgnoreCase;
115function* split(s, splitter) {
116 let start = 0;
117 while (start < s.length) {
118 let end = s.indexOf(splitter, start);
119 if (end === -1) {
120 end = s.length;
121 }
122 yield s.substring(start, end);
123 start = end + splitter.length;
124 }
125}
126exports.split = split;
127function escapeInvisibleChars(value) {
128 return value.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
129}
130exports.escapeInvisibleChars = escapeInvisibleChars;
131function unescapeInvisibleChars(value) {
132 return value.replace(/\\n/g, '\n').replace(/\\r/g, '\r');
133}
134exports.unescapeInvisibleChars = unescapeInvisibleChars;
135function compare(a, b) {
136 if (a < b) {
137 return -1;
138 }
139 else if (a > b) {
140 return 1;
141 }
142 else {
143 return 0;
144 }
145}
146exports.compare = compare;
147function compareSubstring(a, b, aStart = 0, aEnd = a.length, bStart = 0, bEnd = b.length) {
148 for (; aStart < aEnd && bStart < bEnd; aStart++, bStart++) {
149 const codeA = a.charCodeAt(aStart);
150 const codeB = b.charCodeAt(bStart);
151 if (codeA < codeB) {
152 return -1;
153 }
154 else if (codeA > codeB) {
155 return 1;
156 }
157 }
158 const aLen = aEnd - aStart;
159 const bLen = bEnd - bStart;
160 if (aLen < bLen) {
161 return -1;
162 }
163 else if (aLen > bLen) {
164 return 1;
165 }
166 return 0;
167}
168exports.compareSubstring = compareSubstring;
169function compareIgnoreCase(a, b) {
170 return compareSubstringIgnoreCase(a, b, 0, a.length, 0, b.length);
171}
172exports.compareIgnoreCase = compareIgnoreCase;
173function compareSubstringIgnoreCase(a, b, aStart = 0, aEnd = a.length, bStart = 0, bEnd = b.length) {
174 for (; aStart < aEnd && bStart < bEnd; aStart++, bStart++) {
175 const codeA = a.charCodeAt(aStart);
176 const codeB = b.charCodeAt(bStart);
177 if (codeA === codeB) {
178 // equal
179 continue;
180 }
181 const diff = codeA - codeB;
182 if (diff === 32 && isUpperAsciiLetter(codeB)) { // codeB =[65-90] && codeA =[97-122]
183 continue;
184 }
185 else if (diff === -32 && isUpperAsciiLetter(codeA)) { // codeB =[97-122] && codeA =[65-90]
186 continue;
187 }
188 if (isLowerAsciiLetter(codeA) && isLowerAsciiLetter(codeB)) {
189 //
190 return diff;
191 }
192 else {
193 return compareSubstring(a.toLowerCase(), b.toLowerCase(), aStart, aEnd, bStart, bEnd);
194 }
195 }
196 const aLen = aEnd - aStart;
197 const bLen = bEnd - bStart;
198 if (aLen < bLen) {
199 return -1;
200 }
201 else if (aLen > bLen) {
202 return 1;
203 }
204 return 0;
205}
206exports.compareSubstringIgnoreCase = compareSubstringIgnoreCase;
207//# sourceMappingURL=strings.js.map
\No newline at end of file