Debian Codegraph @

src/com/github/weisj/jsvg/parser/impl/ParserUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2025-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.parser.impl;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28 import org.jetbrains.annotations.Contract;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.geometry.size.Length;
33 import com.github.weisj.jsvg.geometry.size.Unit;
34 import com.github.weisj.jsvg.util.ParserBase;
35
36 public final class ParserUtil {
37
38 private ParserUtil() {}
39
40 private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s");
41
42 public static @NotNull String removeWhiteSpace(@NotNull String value) {
43 return WHITESPACE_PATTERN.matcher(value).replaceAll("");
44 }
45
46 public static int parseInt(@Nullable String value, int fallback) {
47 if (value == null) return fallback;
48 try {
49 return Integer.parseInt(value);
50 } catch (NumberFormatException e) {
51 return fallback;
52 }
53 }
54
55 public static float parseFloat(@Nullable String value, float fallback) {
56 if (value == null) return fallback;
57 try {
58 return Float.parseFloat(value);
59 } catch (NumberFormatException e) {
60 return fallback;
61 }
62 }
63
64 @Contract("_,!null -> !null")
65 public static @Nullable Length parseNumber(@Nullable String value, @Nullable Length fallback) {
66 if (value == null) return fallback;
67 try {
68 return Unit.RAW.valueOf(Float.parseFloat(value));
69 } catch (NumberFormatException e) {
70 return fallback;
71 }
72 }
73
74 public static float @NotNull [] parseFloatList(@Nullable String value) {
75 String[] values = parseStringList(value, SeparatorMode.COMMA_AND_WHITESPACE, new String[0]);
76 float[] ret = new float[values.length];
77 for (int i = 0; i < ret.length; i++) {
78 ret[i] = parseFloat(values[i], 0);
79 }
80 return ret;
81 }
82
83 public static double @NotNull [] parseDoubleList(@Nullable String value) {
84 if (value == null || value.isEmpty()) return new double[0];
85 List<Double> list = new ArrayList<>();
86 ParserBase base = new ParserBase(value, 0);
87 while (base.hasNext()) {
88 list.add(base.nextDouble());
89 base.consumeWhiteSpaceOrSeparator();
90 }
91 return list.stream().mapToDouble(Double::doubleValue).toArray();
92 }
93
94 public static @NotNull String @NotNull [] parseStringList(@Nullable String value, @NotNull ListSplitter splitter) {
95 return parseStringList(value, splitter, new String[0]);
96 }
97
98 @Contract("_,_,!null -> !null")
99 public static @NotNull String @Nullable [] parseStringList(@Nullable String value, @NotNull ListSplitter splitter,
100 @NotNull String @Nullable [] fallback) {
101 if (value == null || value.isEmpty()) return fallback;
102 List<String> list = new ArrayList<>();
103 int max = value.length();
104 boolean splitOnWhitespace = splitter.splitOnWhitespace();
105 int start = 0;
106 int i = 0;
107 boolean inWhiteSpace = false;
108 boolean lastSplitWasWhiteSpace = false;
109 for (; i < max; i++) {
110 char c = value.charAt(i);
111 if (Character.isWhitespace(c)) {
112 if (splitOnWhitespace) {
113 if (!inWhiteSpace && i - start > 0) {
114 list.add(value.substring(start, i));
115 lastSplitWasWhiteSpace = true;
116 }
117 start = i + 1;
118 }
119 inWhiteSpace = true;
120 continue;
121 }
122 inWhiteSpace = false;
123 ListSplitter.SplitResult result = splitter.testChar(c, i - start, value, i);
124 if (result.shouldSplit()) {
125 boolean tokenAlreadyEmittedByWhitespace = lastSplitWasWhiteSpace && i == start;
126 if (!tokenAlreadyEmittedByWhitespace) {
127 list.add(value.substring(start, i));
128 }
129 start = result.shouldIncludeChar() ? i : i + 1;
130 }
131 lastSplitWasWhiteSpace = false;
132 }
133 if (i - start > 0) list.add(value.substring(start, i));
134 return list.toArray(new String[0]);
135 }
136 }