@formatjs/icu-messageformat-parser
Advanced tools
+49
-0
@@ -1414,2 +1414,33 @@ import { parseDateTimeSkeleton, parseNumberSkeleton, parseNumberSkeletonFromString } from "@formatjs/icu-skeleton-parser"; | ||
| } | ||
| function plainTopLevelEndPosition(message) { | ||
| if (message.length === 0) return null; | ||
| let line = 1; | ||
| let column = 1; | ||
| for (let offset = 0; offset < message.length;) { | ||
| const code = message.charCodeAt(offset); | ||
| switch (code) { | ||
| case 35: | ||
| case 39: | ||
| case 60: | ||
| case 123: | ||
| case 125: return null; | ||
| } | ||
| if (code === 10) { | ||
| line++; | ||
| column = 1; | ||
| offset++; | ||
| } else { | ||
| column++; | ||
| if (code >= 55296 && code <= 56319 && offset + 1 < message.length) { | ||
| const next = message.charCodeAt(offset + 1); | ||
| offset += next >= 56320 && next <= 57343 ? 2 : 1; | ||
| } else offset++; | ||
| } | ||
| } | ||
| return { | ||
| offset: message.length, | ||
| line, | ||
| column | ||
| }; | ||
| } | ||
| var Parser = class { | ||
@@ -1430,2 +1461,20 @@ constructor(message, options = {}) { | ||
| if (this.offset() !== 0) throw Error("parser can only be used once"); | ||
| if (this.message.length > 0) { | ||
| const firstCode = this.message.charCodeAt(0); | ||
| if (firstCode !== 35 && firstCode !== 39 && firstCode !== 60 && firstCode !== 123 && firstCode !== 125) { | ||
| const plainEndPosition = plainTopLevelEndPosition(this.message); | ||
| if (plainEndPosition) { | ||
| const start = this.clonePosition(); | ||
| this.position = plainEndPosition; | ||
| return { | ||
| val: [{ | ||
| type: 0, | ||
| value: this.message, | ||
| location: createLocation(start, this.clonePosition()) | ||
| }], | ||
| err: null | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| return this.parseMessage(0, "", false); | ||
@@ -1432,0 +1481,0 @@ } |
+9
-9
| { | ||
| "name": "@formatjs/icu-messageformat-parser", | ||
| "version": "3.5.10", | ||
| "version": "3.5.11", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/formatjs/formatjs.git", | ||
| "directory": "packages/icu-messageformat-parser" | ||
| }, | ||
| "type": "module", | ||
@@ -9,14 +14,9 @@ "types": "index.d.ts", | ||
| ".": "./index.js", | ||
| "./manipulator.js": "./manipulator.js", | ||
| "./no-parser.js": "./no-parser.js", | ||
| "./printer.js": "./printer.js", | ||
| "./manipulator.js": "./manipulator.js" | ||
| "./printer.js": "./printer.js" | ||
| }, | ||
| "dependencies": { | ||
| "@formatjs/icu-skeleton-parser": "2.1.9" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/formatjs/formatjs.git", | ||
| "directory": "packages/icu-messageformat-parser" | ||
| "@formatjs/icu-skeleton-parser": "2.1.10" | ||
| } | ||
| } |
+13
-11
@@ -18,6 +18,6 @@ # MessageFormat Parser | ||
| complex_msg: 23.85 µs (41,931 ops/sec) | ||
| normal_msg: 3.27 µs (306,177 ops/sec) | ||
| simple_msg: 0.60 µs (1,675,766 ops/sec) | ||
| string_msg: 0.32 µs (3,113,287 ops/sec) | ||
| complex_msg: 21.39 µs (48,112 ops/sec) | ||
| normal_msg: 3.10 µs (337,642 ops/sec) | ||
| simple_msg: 0.54 µs (1,910,194 ops/sec) | ||
| string_msg: 0.15 µs (7,461,955 ops/sec) | ||
| ``` | ||
@@ -27,12 +27,14 @@ | ||
| The Rust parser (optimized build) is **2.6-3.7x faster** than the JavaScript parser: | ||
| The Rust parser (optimized build) is **2.3-3.5x faster** than the JavaScript parser: | ||
| ``` | ||
| $ bazel run -c opt //crates/icu_messageformat_parser:comparison_bench | ||
| complex_msg: 9.22 µs (2.59x faster than JS) | ||
| normal_msg: 1.14 µs (2.87x faster than JS) | ||
| simple_msg: 163 ns (3.68x faster than JS) | ||
| string_msg: 118 ns (2.71x faster than JS) | ||
| $ bazel run -c opt //crates/icu_messageformat_parser:parser_bench -- --bench --output-format bencher | ||
| complex_msg: 9.43 µs (2.27x faster than JS) | ||
| normal_msg: 1.18 µs (2.63x faster than JS) | ||
| simple_msg: 153 ns (3.55x faster than JS) | ||
| string_msg: 59 ns (2.47x faster than JS) | ||
| ``` | ||
| The Rust parser is also 10-11% faster than the SWC ICU MessageFormat parser. | ||
| The Rust parser is also faster than the SWC ICU MessageFormat parser in this | ||
| benchmark, ranging from 6% faster on `normal_msg` to 2.14x faster on | ||
| `string_msg`. |
Sorry, the diff of this file is too big to display
243939
1.57%3620
1.37%39
5.41%+ Added
- Removed