File: UnicodeString.hx

package info (click to toggle)
haxe 1%3A4.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,556 kB
  • sloc: ml: 125,171; ansic: 2,408; makefile: 436; java: 362; cs: 323; cpp: 318; python: 316; sh: 75; objc: 64; php: 39; xml: 30; javascript: 11
file content (443 lines) | stat: -rw-r--r-- 12,263 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Copyright (C)2005-2019 Haxe Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

import haxe.io.Bytes;
import haxe.io.Encoding;
import haxe.iterators.StringIteratorUnicode;
import haxe.iterators.StringKeyValueIteratorUnicode;

/**
	This abstract provides consistent cross-target unicode support.

	@see https://haxe.org/manual/std-UnicodeString.html
**/
@:forward
@:access(StringTools)
abstract UnicodeString(String) from String to String {
	/**
		Tells if `b` is a correctly encoded UTF8 byte sequence.
	**/
	static public function validate(b:Bytes, encoding:Encoding):Bool {
		switch (encoding) {
			case RawNative:
				throw "UnicodeString.validate: RawNative encoding is not supported";
			case UTF8:
				var data = b.getData();
				var pos = 0;
				var max = b.length;
				while (pos < max) {
					var c:Int = Bytes.fastGet(data, pos++);
					if (c < 0x80) {} else if (c < 0xC2) {
						return false;
					} else if (c < 0xE0) {
						if (pos + 1 > max) {
							return false;
						}
						var c2:Int = Bytes.fastGet(data, pos++);
						if (c2 < 0x80 || c2 > 0xBF) {
							return false;
						}
					} else if (c < 0xF0) {
						if (pos + 2 > max) {
							return false;
						}
						var c2:Int = Bytes.fastGet(data, pos++);
						if (c == 0xE0) {
							if (c2 < 0xA0 || c2 > 0xBF)
								return false;
						} else {
							if (c2 < 0x80 || c2 > 0xBF)
								return false;
						}
						var c3:Int = Bytes.fastGet(data, pos++);
						if (c3 < 0x80 || c3 > 0xBF) {
							return false;
						}
						c = (c << 16) | (c2 << 8) | c3;
						if (0xEDA080 <= c && c <= 0xEDBFBF) { // surrogate pairs
							return false;
						}
					} else if (c > 0xF4) {
						return false;
					} else {
						if (pos + 3 > max) {
							return false;
						}
						var c2:Int = Bytes.fastGet(data, pos++);
						if (c == 0xF0) {
							if (c2 < 0x90 || c2 > 0xBF)
								return false;
						} else if (c == 0xF4) {
							if (c2 < 0x80 || c2 > 0x8F)
								return false;
						} else {
							if (c2 < 0x80 || c2 > 0xBF)
								return false;
						}
						var c3:Int = Bytes.fastGet(data, pos++);
						if (c3 < 0x80 || c3 > 0xBF) {
							return false;
						}
						var c4:Int = Bytes.fastGet(data, pos++);
						if (c4 < 0x80 || c4 > 0xBF) {
							return false;
						}
					}
				}
				return true;
		}
	}

	#if target.unicode
	/**
		Creates an instance of UnicodeString.
	**/
	public inline function new(string:String):Void {
		this = string;
	}

	/**
		Returns an iterator of the unicode code points.
	**/
	public inline function iterator():StringIteratorUnicode {
		return new StringIteratorUnicode(this);
	}

	/**
		Returns an iterator of the code point indices and unicode code points.
	**/
	public inline function keyValueIterator():StringKeyValueIteratorUnicode {
		return new StringKeyValueIteratorUnicode(this);
	}

	#if target.utf16
	/**
		The number of characters in `this` String.
	**/
	public var length(get, never):Int;

	/**
		Returns the character at position `index` of `this` String.

		If `index` is negative or exceeds `this.length`, the empty String `""`
		is returned.
	**/
	public function charAt(index:Int):String {
		if (index < 0)
			return '';
		var unicodeOffset = 0;
		var nativeOffset = 0;
		while (nativeOffset < this.length) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset++);
			if (unicodeOffset == index) {
				return String.fromCharCode(c);
			}
			if (c >= StringTools.MIN_SURROGATE_CODE_POINT) {
				nativeOffset++;
			}
			unicodeOffset++;
		}
		return '';
	}

	/**
		Returns the character code at position `index` of `this` String.

		If `index` is negative or exceeds `this.length`, `null` is returned.
	**/
	public function charCodeAt(index:Int):Null<Int> {
		if (index < 0)
			return null;
		var unicodeOffset = 0;
		var nativeOffset = 0;
		while (nativeOffset < this.length) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset++);
			if (unicodeOffset == index) {
				return c;
			}
			if (c >= StringTools.MIN_SURROGATE_CODE_POINT) {
				nativeOffset++;
			}
			unicodeOffset++;
		}
		return null;
	}

	/**
		Returns the position of the leftmost occurrence of `str` within `this`
		String.

		If `startIndex` is given, the search is performed within the substring
		of `this` String starting from `startIndex` (if `startIndex` is posivite
		or 0) or `max(this.length + startIndex, 0)` (if `startIndex` is negative).

		If `startIndex` exceeds `this.length`, -1 is returned.

		Otherwise the search is performed within `this` String. In either case,
		the returned position is relative to the beginning of `this` String.

		If `str` cannot be found, -1 is returned.
	**/
	public function indexOf(str:String, ?startIndex:Int):Int {
		if (startIndex == null) {
			startIndex = 0;
		} else {
			if (startIndex < 0) {
				startIndex = (this : UnicodeString).length + startIndex;
			}
		}

		var unicodeOffset = 0;
		var nativeOffset = 0;
		var matchingOffset = 0;
		var result = -1;
		while (nativeOffset <= this.length) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset);

			if (unicodeOffset >= startIndex) {
				var c2 = StringTools.utf16CodePointAt(str, matchingOffset);
				if (c == c2) {
					if (matchingOffset == 0) {
						result = unicodeOffset;
					}
					matchingOffset++;
					if (c2 >= StringTools.MIN_SURROGATE_CODE_POINT) {
						matchingOffset++;
					}
					if (matchingOffset == str.length) {
						return result;
					}
				} else if (matchingOffset != 0) {
					result = -1;
					matchingOffset = 0;
					continue;
				}
			}

			nativeOffset++;
			if (c >= StringTools.MIN_SURROGATE_CODE_POINT) {
				nativeOffset++;
			}
			unicodeOffset++;
		}
		return -1;
	}

	/**
		Returns the position of the rightmost occurrence of `str` within `this`
		String.

		If `startIndex` is given, the search is performed within the substring
		of `this` String from 0 to `startIndex + str.length`. Otherwise the search
		is performed within `this` String. In either case, the returned position
		is relative to the beginning of `this` String.

		If `str` cannot be found, -1 is returned.
	**/
	public function lastIndexOf(str:String, ?startIndex:Int):Int {
		if (startIndex == null) {
			startIndex = this.length;
		} else if (startIndex < 0) {
			startIndex = 0;
		}

		var unicodeOffset = 0;
		var nativeOffset = 0;
		var result = -1;
		var lastIndex = -1;
		var matchingOffset = 0;
		var strUnicodeLength = (str : UnicodeString).length;
		while (nativeOffset < this.length && unicodeOffset < startIndex + strUnicodeLength) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset);

			var c2 = StringTools.utf16CodePointAt(str, matchingOffset);
			if (c == c2) {
				if (matchingOffset == 0) {
					lastIndex = unicodeOffset;
				}
				matchingOffset++;
				if (c2 >= StringTools.MIN_SURROGATE_CODE_POINT) {
					matchingOffset++;
				}
				if (matchingOffset == str.length) {
					result = lastIndex;
					lastIndex = -1;
				}
			} else if (matchingOffset != 0) {
				lastIndex = -1;
				matchingOffset = 0;
				continue;
			}

			nativeOffset++;
			if (c >= StringTools.MIN_SURROGATE_CODE_POINT) {
				nativeOffset++;
			}
			unicodeOffset++;
		}
		return result;
	}

	/**
		Returns `len` characters of `this` String, starting at position `pos`.

		If `len` is omitted, all characters from position `pos` to the end of
		`this` String are included.

		If `pos` is negative, its value is calculated from the end of `this`
		String by `this.length + pos`. If this yields a negative value, 0 is
		used instead.

		If the calculated position + `len` exceeds `this.length`, the characters
		from that position to the end of `this` String are returned.

		If `len` is negative, the result is unspecified.
	**/
	public function substr(pos:Int, ?len:Int):String {
		if (pos < 0) {
			pos = (this : UnicodeString).length + pos;
			if (pos < 0) {
				pos = 0;
			}
		}
		if (len != null) {
			if (len < 0) {
				len = (this : UnicodeString).length + len;
			}
			if (len <= 0) {
				return "";
			}
		}
		var unicodeOffset = 0;
		var nativeOffset = 0;
		var fromOffset = -1;
		var subLength = 0;
		while (nativeOffset < this.length) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset);

			if (unicodeOffset >= pos) {
				if (fromOffset < 0) {
					if (len == null) {
						return this.substr(nativeOffset);
					}
					fromOffset = nativeOffset;
				}
				subLength++;
				if (subLength >= len) {
					var lastOffset = (c < StringTools.MIN_SURROGATE_CODE_POINT ? nativeOffset : nativeOffset + 1);
					return this.substr(fromOffset, lastOffset - fromOffset + 1);
				}
			}

			nativeOffset += (c >= StringTools.MIN_SURROGATE_CODE_POINT ? 2 : 1);
			unicodeOffset++;
		}
		return (fromOffset < 0 ? "" : this.substr(fromOffset));
	}

	/**
		Returns the part of `this` String from `startIndex` to but not including `endIndex`.

		If `startIndex` or `endIndex` are negative, 0 is used instead.

		If `startIndex` exceeds `endIndex`, they are swapped.

		If the (possibly swapped) `endIndex` is omitted or exceeds
		`this.length`, `this.length` is used instead.

		If the (possibly swapped) `startIndex` exceeds `this.length`, the empty
		String `""` is returned.
	**/
	public function substring(startIndex:Int, ?endIndex:Int):String {
		if (startIndex < 0) {
			startIndex = 0;
		}
		if (endIndex != null) {
			if (endIndex < 0) {
				endIndex = 0;
			}
			if (startIndex == endIndex) {
				return "";
			}
			if (startIndex > endIndex) {
				var tmp = startIndex;
				startIndex = endIndex;
				endIndex = tmp;
			}
		}

		var unicodeOffset = 0;
		var nativeOffset = 0;
		var fromOffset = -1;
		var subLength = 0;
		while (nativeOffset < this.length) {
			var c = StringTools.utf16CodePointAt(this, nativeOffset);

			if (startIndex <= unicodeOffset) {
				if (fromOffset < 0) {
					if (endIndex == null) {
						return this.substr(nativeOffset);
					}
					fromOffset = nativeOffset;
				}
				subLength++;
				if (subLength >= endIndex - startIndex) {
					var lastOffset = (c < StringTools.MIN_SURROGATE_CODE_POINT ? nativeOffset : nativeOffset + 1);
					return this.substr(fromOffset, lastOffset - fromOffset + 1);
				}
			}

			nativeOffset += (c >= StringTools.MIN_SURROGATE_CODE_POINT ? 2 : 1);
			unicodeOffset++;
		}
		return (fromOffset < 0 ? "" : this.substr(fromOffset));
	}

	function get_length():Int {
		var l = 0;
		for (c in new StringIteratorUnicode(this)) {
			l++;
		}
		return l;
	}
	#end
	#end
	@:op(A < B) static function lt(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A <= B) static function lte(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A > B) static function gt(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A >= B) static function gte(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A == B) static function eq(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A != B) static function neq(a:UnicodeString, b:UnicodeString):Bool;

	@:op(A + B) static function add(a:UnicodeString, b:UnicodeString):UnicodeString;

	@:op(A += B) static function assignAdd(a:UnicodeString, b:UnicodeString):UnicodeString;

	@:op(A + B) @:commutative static function add(a:UnicodeString, b:String):UnicodeString;

	@:op(A += B) @:commutative static function assignAdd(a:UnicodeString, b:String):UnicodeString;
}