Skip to content

Commit 32fcb8c

Browse files
authored
Sync with underscore-java
1 parent 269ad67 commit 32fcb8c

File tree

4 files changed

+94
-23
lines changed

4 files changed

+94
-23
lines changed

src/main/java/com/github/underscore/Xml.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,30 +1521,26 @@ private static Object addElement(
15211521
}
15221522

15231523
static Map<String, String> parseAttributes(final String source) {
1524-
final Map<String, String> result = new LinkedHashMap<>();
1525-
final StringBuilder key = new StringBuilder();
1526-
final StringBuilder value = new StringBuilder();
1527-
boolean quoteFound = false;
1528-
boolean equalFound = false;
1529-
for (int index = 0; index < source.length(); index += 1) {
1530-
if (source.charAt(index) == '=') {
1531-
equalFound = !equalFound;
1532-
continue;
1533-
}
1534-
if (source.charAt(index) == '"') {
1535-
if (quoteFound && equalFound) {
1524+
Map<String, String> result = new LinkedHashMap<>();
1525+
StringBuilder key = new StringBuilder();
1526+
StringBuilder value = new StringBuilder();
1527+
boolean inQuotes = false;
1528+
boolean expectingValue = false;
1529+
for (char c : source.toCharArray()) {
1530+
if (c == '"') {
1531+
inQuotes = !inQuotes;
1532+
if (!inQuotes && expectingValue) {
15361533
result.put(key.toString(), value.toString());
15371534
key.setLength(0);
15381535
value.setLength(0);
1539-
equalFound = false;
1540-
}
1541-
quoteFound = !quoteFound;
1542-
} else if (quoteFound || SKIPPED_CHARS.contains(source.charAt(index))) {
1543-
if (quoteFound) {
1544-
value.append(source.charAt(index));
1536+
expectingValue = false;
15451537
}
1546-
} else {
1547-
key.append(source.charAt(index));
1538+
} else if (c == '=' && !inQuotes) {
1539+
expectingValue = true;
1540+
} else if (inQuotes) {
1541+
value.append(c);
1542+
} else if (!SKIPPED_CHARS.contains(c)) {
1543+
key.append(c);
15481544
}
15491545
}
15501546
return result;

src/test/java/com/github/underscore/FunctionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void defer() {
199199
return null;
200200
});
201201
assertEquals(0, counter[0].intValue(), "incr was debounced");
202-
await().atMost(120, TimeUnit.MILLISECONDS)
202+
await().atLeast(90, TimeUnit.MILLISECONDS)
203203
.until(
204204
() -> {
205205
assertEquals(1, counter[0].intValue(), "incr was debounced");

src/test/java/com/github/underscore/LodashTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,19 @@ void xmpToJson4() {
10281028
+ "</z:catalog>"));
10291029
}
10301030

1031+
@Test
1032+
void xmpToJson5() {
1033+
assertEquals("{\n"
1034+
+ " \"Comment\": {\n"
1035+
+ " \"-stringValue\": \"============================\",\n"
1036+
+ " \"-self-closing\": \"true\"\n"
1037+
+ " },\n"
1038+
+ " \"#omit-xml-declaration\": \"yes\"\n"
1039+
+ "}",
1040+
U.xmlToJson(
1041+
"<Comment stringValue=\"============================\"/>"));
1042+
}
1043+
10311044
@Test
10321045
void xmlToJsonMinimum() {
10331046
assertEquals(

src/test/java/com/github/underscore/StringTest.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,9 +2358,71 @@ void parseAttributes() {
23582358
"{version=1.0}",
23592359
Xml.parseAttributes(" version = \"1.0\" encoding= \"UTF-8 ").toString());
23602360
assertEquals(
2361-
"{}", Xml.parseAttributes(" version = \"1.0 encoding= \"UTF-8\" ").toString());
2361+
"{version=1.0 encoding= }",
2362+
Xml.parseAttributes(" version = \"1.0 encoding= \"UTF-8\" ").toString());
23622363
assertEquals(
2363-
"{}", Xml.parseAttributes(" version = 1.0\" encoding= \"UTF-8\" ").toString());
2364+
"{version1.0= encoding= }",
2365+
Xml.parseAttributes(" version = 1.0\" encoding= \"UTF-8\" ").toString());
2366+
}
2367+
2368+
@Test
2369+
void testSingleAttribute() {
2370+
Map<String, String> result = Xml.parseAttributes("key1=\"value1\"");
2371+
assertEquals(Map.of("key1", "value1"), result);
2372+
}
2373+
2374+
@Test
2375+
void testMultipleAttributes() {
2376+
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"value2\"");
2377+
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
2378+
}
2379+
2380+
@Test
2381+
void testAttributeWithSpaces() {
2382+
Map<String, String> result = Xml.parseAttributes("key1=\"value with spaces\" key2=\"another value\"");
2383+
assertEquals(Map.of("key1", "value with spaces", "key2", "another value"), result);
2384+
}
2385+
2386+
@Test
2387+
void testEmptyValue() {
2388+
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"\"");
2389+
assertEquals(Map.of("key1", "value1", "key2", ""), result);
2390+
}
2391+
2392+
@Test
2393+
void testAttributesWithoutSpaceSeparation() {
2394+
Map<String, String> result = Xml.parseAttributes("key1=\"value1\"key2=\"value2\"");
2395+
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
2396+
}
2397+
2398+
@Test
2399+
void testUnclosedQuotes() {
2400+
Map<String, String> result = Xml.parseAttributes("key1=\"value1 key2=\"value2\"");
2401+
assertEquals(Map.of("key1", "value1 key2="), result);
2402+
}
2403+
2404+
@Test
2405+
void testEqualsSignInValue() {
2406+
Map<String, String> result = Xml.parseAttributes("key1=\"value=1\" key2=\"value=2\"");
2407+
assertEquals(Map.of("key1", "value=1", "key2", "value=2"), result);
2408+
}
2409+
2410+
@Test
2411+
void testTrailingWhitespace() {
2412+
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"value2\" ");
2413+
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
2414+
}
2415+
2416+
@Test
2417+
void testLeadingWhitespace() {
2418+
Map<String, String> result = Xml.parseAttributes(" key1=\"value1\" key2=\"value2\"");
2419+
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
2420+
}
2421+
2422+
@Test
2423+
void testNoEqualsSign() {
2424+
Map<String, String> result = Xml.parseAttributes("key1\"value1\" key2=\"value2\"");
2425+
assertEquals(Map.of("key1key2", "value1value2"), result);
23642426
}
23652427

23662428
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)