Plex/proxy/src/main/java/dev/plex/toml/MultilineStringValueReader.java

67 lines
1.9 KiB
Java
Raw Permalink Normal View History

package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger;
2022-05-04 12:19:34 +00:00
class MultilineStringValueReader implements ValueReader
{
2022-05-10 05:08:45 +00:00
static final MultilineStringValueReader MULTILINE_STRING_VALUE_READER = new MultilineStringValueReader();
@Override
public boolean canRead(String s)
{
return s.startsWith("\"\"\"");
}
2022-05-10 05:08:45 +00:00
@Override
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
{
AtomicInteger line = context.line;
int startLine = line.get();
int originalStartIndex = index.get();
int startIndex = index.addAndGet(3);
int endIndex = -1;
if (s.charAt(startIndex) == '\n')
{
startIndex = index.incrementAndGet();
line.incrementAndGet();
}
for (int i = startIndex; i < s.length(); i = index.incrementAndGet())
{
char c = s.charAt(i);
2022-05-10 05:08:45 +00:00
if (c == '\n')
{
line.incrementAndGet();
}
else if (c == '"' && s.length() > i + 2 && s.charAt(i + 1) == '"' && s.charAt(i + 2) == '"')
{
endIndex = i;
index.addAndGet(2);
break;
}
}
2022-05-10 05:08:45 +00:00
if (endIndex == -1)
{
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
return errors;
}
s = s.substring(startIndex, endIndex);
s = s.replaceAll("\\\\\\s+", "");
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
return s;
}
private MultilineStringValueReader()
{
}
}