Skip to content

Commit 2c6082a

Browse files
committed
Refactored stop conditions to be invariant by using while loop.
1 parent 5dc1031 commit 2c6082a

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/main/java/org/json/Cookie.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,30 @@ public static String toString(JSONObject jo) throws JSONException {
189189
* @return The unescaped string.
190190
*/
191191
public static String unescape(String string) {
192+
int i = 0;
192193
int length = string.length();
193194
StringBuilder sb = new StringBuilder(length);
194-
for (int i = 0; i < length; ++i) {
195+
196+
while (i < length) {
195197
char c = string.charAt(i);
196198
if (c == '+') {
197-
c = ' ';
199+
sb.append(' ');
200+
i++;
198201
} else if (c == '%' && i + 2 < length) {
199202
int d = JSONTokener.dehexchar(string.charAt(i + 1));
200203
int e = JSONTokener.dehexchar(string.charAt(i + 2));
204+
201205
if (d >= 0 && e >= 0) {
202-
c = (char)(d * 16 + e);
203-
i += 2;
206+
sb.append((char)(d * 16 + e));
207+
i += 3;
208+
} else {
209+
sb.append(c);
210+
i++;
204211
}
212+
} else {
213+
sb.append(c);
214+
i++;
205215
}
206-
sb.append(c);
207216
}
208217
return sb.toString();
209218
}

0 commit comments

Comments
 (0)