structurae
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -157,9 +157,18 @@ // Type definitions for structurae | ||
export declare class StringView extends Uint8Array { | ||
size: number; | ||
static masks: Int8Array; | ||
characters(): string; | ||
charAt(index?: number): string; | ||
private getCharEnd(index: number): number; | ||
private getCharStart(index: number, startCharIndex?: number, startIndex?: number): number; | ||
replace(pattern: Collection, replacement: Collection): this; | ||
reverse(): this; | ||
search(searchValue: Collection, fromIndex?: number): number; | ||
substring(indexStart: number, indexEnd?: number): string; | ||
private toChar(index: number): string; | ||
toString(): string; | ||
trim(): StringView; | ||
static fromString(string: string, size?: number): StringView; | ||
static getByteSize(string: string): number; | ||
} |
@@ -13,2 +13,59 @@ const { searchNaive, searchShiftOr } = require('./algorithms'); | ||
/** | ||
* Iterates over the characters in the StringView. | ||
* | ||
* @yields {string} | ||
*/ | ||
* characters() { | ||
for (let i = 0; i < this.length; i++) { | ||
if (this[i] >> 6 !== 2) { | ||
yield this.toChar(i); | ||
} | ||
} | ||
} | ||
/** | ||
* Returns a new string consisting of the single UTF character | ||
* located at the specified character index. | ||
* | ||
* @param {number} [index=0] a character index | ||
* @returns {string} a string representing the character | ||
*/ | ||
charAt(index = 0) { | ||
return this.toChar(this.getCharStart(index)); | ||
} | ||
/** | ||
* @private | ||
* @param {number} index | ||
* @returns {number} | ||
*/ | ||
getCharEnd(index) { | ||
const point = this[index]; | ||
if (point < 0x80) return index; | ||
switch ((point & 0xF0) >> 4) { | ||
case 0xF: return index + 3; | ||
case 0xE: return index + 2; | ||
case 0xD: | ||
case 0xC: return index + 1; | ||
default: return -1; | ||
} | ||
} | ||
/** | ||
* @private | ||
* @param {number} index | ||
* @param {number} [startCharIndex=-1] | ||
* @param {number} [startIndex=0] | ||
* @returns {number} | ||
*/ | ||
getCharStart(index, startCharIndex = -1, startIndex = 0) { | ||
let current = startCharIndex; | ||
for (let i = startIndex; i < this.length; i++) { | ||
if (this[i] >> 6 !== 2) current++; | ||
if (current === index) return i; | ||
} | ||
return -1; | ||
} | ||
/** | ||
* Performs an in-place replacement within the StringView | ||
@@ -70,3 +127,3 @@ * of all occurrences of a given pattern with a given replacement. | ||
* @param {Collection} searchValue the value to search for | ||
* @param {number} [fromIndex] the index at which to start the search | ||
* @param {number} [fromIndex=0] the index at which to start the search | ||
* @returns {number} the index of the first occurrence of the specified value | ||
@@ -82,2 +139,53 @@ */ | ||
/** | ||
* The amount of UTF characters in the StringView. | ||
* | ||
* @type {number} | ||
*/ | ||
get size() { | ||
let size = 0; | ||
for (let i = 0; i < this.length; i++) { | ||
if ((this[i] >> 6) !== 2) size++; | ||
} | ||
return size; | ||
} | ||
/** | ||
* Returns a string of characters between the start and end | ||
* character indexes, or to the end of the string. | ||
* | ||
* @param {number} indexStart the character index of the first character to include | ||
* @param {number} [indexEnd] the character index of the first character to exclude | ||
* @returns {string} a new string containing the specified part of the given string | ||
*/ | ||
substring(indexStart, indexEnd = this.size) { | ||
const start = this.getCharStart(indexStart); | ||
const end = this.getCharStart(indexEnd, indexStart, start); | ||
const sub = this.subarray(start, this.getCharEnd(end) + 1); | ||
return Decoder.decode(sub); | ||
} | ||
/** | ||
* @private | ||
* @param {number} index | ||
* @returns {string} | ||
*/ | ||
toChar(index) { | ||
const point = this[index]; | ||
if (point < 0x80) return String.fromCodePoint(point); | ||
switch ((point & 0xF0) >> 4) { | ||
case 0xF: return String.fromCodePoint(((point & 0x07) << 18) | ||
| ((this[index + 1] & 0x3F) << 12) | ||
| ((this[index + 2] & 0x3F) << 6) | ||
| ((this[index + 3] & 0x3F))); | ||
case 0xE: return String.fromCodePoint(((point & 0x0F) << 12) | ||
| ((this[index + 1] & 0x3F) << 6) | ||
| ((this[index + 2] & 0x3F))); | ||
case 0xD: | ||
case 0xC: return String.fromCodePoint(((point & 0x1F) << 6) | ||
| ((this[index + 1] & 0x3F))); | ||
default: return ''; | ||
} | ||
} | ||
/** | ||
* Returns a string representation of the StringView. | ||
@@ -124,3 +232,3 @@ * | ||
*/ | ||
static getStringSize(string) { | ||
static getByteSize(string) { | ||
let size = 0; | ||
@@ -127,0 +235,0 @@ for (let i = 0; i < string.length; i++) { |
{ | ||
"name": "structurae", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76442
2006