Skip to content

Commit e7f2859

Browse files
author
Mark H
committed
Use Dart's built in runes functionality in punycode algorithm. Fixes #15
1 parent 061fcfe commit e7f2859

File tree

2 files changed

+2
-38
lines changed

2 files changed

+2
-38
lines changed

lib/src/punycode/encoder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class PunycodeEncoder extends Converter<String, String> {
5555
String convert(String input) {
5656
final output = <int>[];
5757

58-
// Convert the input in UCS-2 to an array of Unicode code points.
59-
final decodedInput = ucs2decode(input);
58+
// Convert the input to an array of Unicode code points.
59+
final decodedInput = input.runes.toList();
6060

6161
// Cache the length.
6262
final inputLength = decodedInput.length;

lib/src/punycode/shared.dart

-36
Original file line numberDiff line numberDiff line change
@@ -62,42 +62,6 @@ int adapt({
6262
(delta + bootstrapValues.skew));
6363
}
6464

65-
/// Decodes a string into a list of Unicode code points.
66-
///
67-
/// This function handles surrogate pairs to correctly represent Unicode
68-
/// characters beyond the Basic Multilingual Plane (BMP). This
69-
/// function combines surrogate pairs into single code points, matching UTF-16
70-
/// encoding.
71-
///
72-
/// Args:
73-
/// string: The input string to decode.
74-
///
75-
/// Returns:
76-
/// A [List<int>] where each element represents a Unicode code point.
77-
List<int> ucs2decode(String string) {
78-
final output = <int>[];
79-
var counter = 0;
80-
while (counter < string.length) {
81-
final value = string.codeUnitAt(counter++);
82-
if (value >= 0xD800 && value <= 0xDBFF && counter < string.length) {
83-
// It's a high surrogate, and there is a next character.
84-
final extra = string.codeUnitAt(counter++) - 0xDC00 + 0x10000;
85-
if ((extra & 0xFC00) == 0xDC00) {
86-
// Low surrogate.
87-
output.add(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
88-
} else {
89-
// It's an unmatched surrogate; only append this code unit, in case the
90-
// next code unit is the high surrogate of a surrogate pair.
91-
output.add(value);
92-
counter--;
93-
}
94-
} else {
95-
output.add(value);
96-
}
97-
}
98-
return output;
99-
}
100-
10165
/// Converts a digit/integer into a basic code point.
10266
///
10367
/// See `basicToDigit()`

0 commit comments

Comments
 (0)