Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2025 Jannis Weis
* Copyright (c) 2025-2026 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand Down Expand Up @@ -31,6 +31,7 @@

import com.github.weisj.jsvg.geometry.size.Length;
import com.github.weisj.jsvg.geometry.size.Unit;
import com.github.weisj.jsvg.parser.NumberListSplitter;
import com.github.weisj.jsvg.util.ParserBase;

public final class ParserUtil {
Expand Down Expand Up @@ -101,27 +102,44 @@ public static float parseFloat(@Nullable String value, float fallback) {
if (value == null || value.isEmpty()) return fallback;
List<String> list = new ArrayList<>();
int max = value.length();
boolean splitOnWhitespace = splitter.splitOnWhitespace();
boolean trimTokens = splitOnWhitespace && splitter instanceof NumberListSplitter;
int start = 0;
int i = 0;
boolean inWhiteSpace = false;
boolean lastSplitWasWhiteSpace = false;
for (; i < max; i++) {
char c = value.charAt(i);
if (Character.isWhitespace(c)) {
if (!inWhiteSpace && splitter.splitOnWhitespace() && i - start > 0) {
list.add(value.substring(start, i));
if (!inWhiteSpace && splitOnWhitespace && i - start > 0) {
String token = value.substring(start, i);
if (trimTokens) token = token.trim();
list.add(token);
start = i + 1;
lastSplitWasWhiteSpace = true;
}
inWhiteSpace = true;
continue;
}
inWhiteSpace = false;
ListSplitter.SplitResult result = splitter.testChar(c, i - start);
if (result.shouldSplit()) {
list.add(value.substring(start, i));
if (!(lastSplitWasWhiteSpace && i == start)) {
String token = value.substring(start, i);
if (trimTokens) token = token.trim();
list.add(token);
}
start = result.shouldIncludeChar() ? i : i + 1;
lastSplitWasWhiteSpace = false;
continue;
}
lastSplitWasWhiteSpace = false;
}
if (i - start > 0) {
String token = value.substring(start, i);
if (trimTokens) token = token.trim();
list.add(token);
}
if (i - start > 0) list.add(value.substring(start, i));
return list.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021-2025 Jannis Weis
* Copyright (c) 2021-2026 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -22,12 +22,14 @@
package com.github.weisj.jsvg.attribute;

import java.util.Random;
import java.util.function.BiConsumer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.weisj.jsvg.paint.impl.DefaultPaintParser;
import com.github.weisj.jsvg.parser.NumberListSplitter;
import com.github.weisj.jsvg.parser.impl.AttributeParser;
import com.github.weisj.jsvg.parser.impl.SeparatorMode;
import com.github.weisj.jsvg.util.RandomData;
Expand All @@ -51,6 +53,21 @@ void testStringListRequiredComma() {
testStringList(true);
}

@Test
void testNumberSplitter() {
BiConsumer<String, String[]> test = (str, expected) -> {
String[] result = parser.parseStringList(str, NumberListSplitter.INSTANCE);
Assertions.assertArrayEquals(expected, result);
};
test.accept("1,1", new String[] {"1", "1"});
test.accept("1 1", new String[] {"1", "1"});
test.accept("1-1", new String[] {"1", "-1"});
test.accept("1, 1", new String[] {"1", "1"});
test.accept("1, 1", new String[] {"1", "1"});
test.accept("1 , 1", new String[] {"1", "1"});
test.accept("1, ,1", new String[] {"1", "", "1"});
}

@Test
void testFloatList() {
Random r = new Random();
Expand Down Expand Up @@ -91,4 +108,5 @@ private <T> String appendToList(T[] arr, Random r, boolean requireComma) {
}
return builder.toString();
}

}