Skip to content

Commit 17592a5

Browse files
lrhnCommit Queue
authored andcommitted
Change internal character set representation in Uri.
Avoid doing computed shifts and parameterizing with tables, so that all lookups can be with a fixed base and only do one bit-operation. Tested: Refactoring, covered by existing tests. Change-Id: I8c4ffdb0eaef6c26de996ca40d1d697c1ba23bb7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392943 Reviewed-by: Stephen Adams <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]> Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 04dfa8a commit 17592a5

File tree

6 files changed

+390
-369
lines changed

6 files changed

+390
-369
lines changed

sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ class _Uri {
704704
/// that appear in [canonicalTable], and returns the escaped string.
705705
@patch
706706
static String _uriEncode(
707-
List<int> canonicalTable,
707+
int canonicalMask,
708708
String text,
709709
Encoding encoding,
710710
bool spaceToPlus,
@@ -719,8 +719,7 @@ class _Uri {
719719
var bytes = encoding.encode(text);
720720
for (int i = 0; i < bytes.length; i++) {
721721
int byte = bytes[i];
722-
if (byte < 128 &&
723-
((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
722+
if (byte < 128 && ((_charTables.codeUnitAt(byte) & canonicalMask) != 0)) {
724723
result.writeCharCode(byte);
725724
} else if (spaceToPlus && byte == _SPACE) {
726725
result.write('+');

sdk/lib/_internal/js_runtime/lib/core_patch.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,8 @@ class _Uri {
692692
/// It encodes all characters in the string [text] except for those
693693
/// that appear in [canonicalTable], and returns the escaped string.
694694
@patch
695-
static String _uriEncode(List<int> canonicalTable, String text,
696-
Encoding encoding, bool spaceToPlus) {
695+
static String _uriEncode(
696+
int canonicalMask, String text, Encoding encoding, bool spaceToPlus) {
697697
if (identical(encoding, utf8) && _needsNoEncoding.hasMatch(text)) {
698698
return text;
699699
}
@@ -704,8 +704,7 @@ class _Uri {
704704
var bytes = encoding.encode(text);
705705
for (int i = 0; i < bytes.length; i++) {
706706
int byte = bytes[i];
707-
if (byte < 128 &&
708-
((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
707+
if (byte < 128 && ((_charTables.codeUnitAt(byte) & canonicalMask) != 0)) {
709708
result.writeCharCode(byte);
710709
} else if (spaceToPlus && byte == _SPACE) {
711710
result.write('+');

sdk/lib/_internal/vm/lib/uri_patch.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class _Uri {
3434

3535
@patch
3636
static String _uriEncode(
37-
List<int> canonicalTable,
37+
int canonicalMask,
3838
String text,
3939
Encoding encoding,
4040
bool spaceToPlus,
@@ -48,8 +48,7 @@ class _Uri {
4848
// Find first character that needs encoding.
4949
for (; i < text.length; i++) {
5050
var char = text.codeUnitAt(i);
51-
if (char >= 128 ||
52-
canonicalTable[char >> 4] & (1 << (char & 0x0f)) == 0) {
51+
if (char >= 128 || _charTables.codeUnitAt(char) & canonicalMask == 0) {
5352
break;
5453
}
5554
}
@@ -67,8 +66,7 @@ class _Uri {
6766
var bytes = encoding.encode(text);
6867
for (; i < bytes.length; i++) {
6968
int byte = bytes[i];
70-
if (byte < 128 &&
71-
((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
69+
if (byte < 128 && ((_charTables.codeUnitAt(byte) & canonicalMask) != 0)) {
7270
result.writeCharCode(byte);
7371
} else if (spaceToPlus && byte == _SPACE) {
7472
result.writeCharCode(_PLUS);

sdk/lib/_internal/wasm/lib/uri_patch.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class _Uri {
3636
}""");
3737

3838
// Matches a String that _uriEncodes to itself regardless of the kind of
39-
// component. This corresponds to [_unreservedTable], i.e. characters that
40-
// are not encoded by any encoding table.
39+
// component. This corresponds to `_unreservedMask` table,
40+
// i.e. characters that are not encoded by any encoding table.
4141
static final RegExp _needsNoEncoding = RegExp(r'^[\-\.0-9A-Z_a-z~]*$');
4242

4343
/**
@@ -47,7 +47,7 @@ class _Uri {
4747
*/
4848
@patch
4949
static String _uriEncode(
50-
List<int> canonicalTable,
50+
int canonicalMask,
5151
String text,
5252
Encoding encoding,
5353
bool spaceToPlus,
@@ -62,8 +62,7 @@ class _Uri {
6262
var bytes = encoding.encode(text);
6363
for (int i = 0; i < bytes.length; i++) {
6464
int byte = bytes[i];
65-
if (byte < 128 &&
66-
((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
65+
if (byte < 128 && ((_charTables.codeUnitAt(byte) & canonicalMask) != 0)) {
6766
result.writeCharCode(byte);
6867
} else if (spaceToPlus && byte == _SPACE) {
6968
result.write('+');

0 commit comments

Comments
 (0)