MCPcopy Create free account
hub / github.com/dynjs/dynjs / encode

Method encode

src/main/java/org/dynjs/runtime/builtins/URLCodec.java:14–77  ·  view source on GitHub ↗
(ExecutionContext context, String str, String unescapedSet)

Source from the content-addressed store, hash-verified

12 public static String URI_UNESCAPED_SET = URI_ALPHA + DECIMAL_DIGIT + URI_MARK;
13
14 public static String encode(ExecutionContext context, String str, String unescapedSet) {
15 int len = str.length();
16
17 StringBuilder r = new StringBuilder();
18
19 int k = 0;
20
21 while (true) {
22 if (k == len) {
23 return r.toString();
24 }
25
26 char c = str.charAt(k);
27
28 if (unescapedSet.contains("" + c)) {
29 r.append(c);
30 } else {
31
32 if ((!(c < 0xDC00)) && (!(c > 0xDFFF))) {
33 throw new ThrowException(context, context.createUriError("invalid escape"));
34 }
35
36 long v = 0;
37
38 if (c < 0xD800 || c > 0xDBFF) {
39 v = c;
40 } else {
41 ++k;
42 if (k == len) {
43 throw new ThrowException(context, context.createUriError("invalid escape"));
44 }
45
46 char kChar = str.charAt(k);
47
48 if (kChar < 0xDC00 || kChar > 0xDFFF) {
49 throw new ThrowException(context, context.createUriError("invalid escape"));
50 }
51
52 v = ((c - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000);
53 }
54
55 if (v < 0x80) {
56 r.append(String.format("%%%02X", v));
57 } else if (v < 0x800) {
58 int o1 = (int) ((v >> 6) | 0xC0);
59 int o2 = (int) ((v & 0x3F) | 0x80);
60 r.append(String.format("%%%02X%%%02X", o1, o2));
61 } else if (v <= 0xFFFF) {
62 int o1 = (int) (((v >> 12) & 0x1F) | 0xE0);
63 int o2 = (int) (((v >> 6) & 0x3F) | 0x80);
64 int o3 = (int) ((v & 0x3F) | 0x80);
65 r.append(String.format("%%%02X%%%02X%%%02X", o1, o2, o3));
66 } else if (v <= 0x10FFFF) {
67 int o1 = (int) (((v >> 18) & 0x07) | 0xF0);
68 int o2 = (int) (((v >> 12) & 0x3F) | 0x80);
69 int o3 = (int) (((v >> 6) & 0x3F) | 0x80);
70 int o4 = (int) ((v & 0x3F) | 0x80);
71 r.append(String.format("%%%02X%%%02X%%%02X%%%02X", o1, o2, o3, o4));

Callers 3

callMethod · 0.95
callMethod · 0.95
setPatternAndFlagsMethod · 0.80

Calls 6

lengthMethod · 0.80
containsMethod · 0.80
appendMethod · 0.80
createUriErrorMethod · 0.80
toStringMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected