Skip to content

Commit

Permalink
Avoid producing garbage in escaping quotes
Browse files Browse the repository at this point in the history
fixes #377
  • Loading branch information
alamar committed Mar 23, 2022
1 parent 29b5ada commit 308f2dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/openhft/chronicle/wire/CSVWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
* YAML Based wire format
*/
public class CSVWire extends TextWire {

private static final ThreadLocal<WeakReference<StopCharTester>> ESCAPED_END_OF_TEXT = new ThreadLocal<>();
static final Supplier<StopCharTester> COMMA_STOP_ESCAPING = StopCharTesters.COMMA_STOP::escaping;

private final List<String> header = new ArrayList<>();

Expand Down Expand Up @@ -65,8 +67,7 @@ public static CSVWire from(@NotNull String text) {

@NotNull
static StopCharTester getEscapingCSVEndOfText() {
StopCharTester escaping = ThreadLocalHelper.getTL(ESCAPED_END_OF_TEXT,
StopCharTesters.COMMA_STOP::escaping);
StopCharTester escaping = ThreadLocalHelper.getTL(ESCAPED_END_OF_TEXT, COMMA_STOP_ESCAPING);
// reset it.
escaping.isStopChar(' ');
return escaping;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/JSONWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.StopCharsTester;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.util.ClassNotFoundRuntimeException;
Expand All @@ -32,6 +33,7 @@
import java.time.LocalTime;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Supplier;

import static net.openhft.chronicle.bytes.NativeBytes.nativeBytes;

Expand All @@ -43,6 +45,7 @@
public class JSONWire extends TextWire {
@SuppressWarnings("rawtypes")
static final BytesStore COMMA = BytesStore.from(",");
static final Supplier<StopCharsTester> STRICT_END_OF_TEXT_JSON_ESCAPING = TextStopCharsTesters.STRICT_END_OF_TEXT_JSON::escaping;
boolean useTypes;

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -201,11 +204,18 @@ protected StringBuilder readField(@NotNull StringBuilder sb) {
}

@Override
@Deprecated(/* To be removed in 2.24 */)
@NotNull
protected TextStopCharsTesters strictEndOfText() {
return TextStopCharsTesters.STRICT_END_OF_TEXT_JSON;
}

@Override
@NotNull
protected Supplier<StopCharsTester> strictEndOfTextEscaping() {
return STRICT_END_OF_TEXT_JSON_ESCAPING;
}

class JSONValueOut extends TextValueOut {
@NotNull
@Override
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/net/openhft/chronicle/wire/TextWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public class TextWire extends AbstractWire implements Wire {
static final BytesStore END_FIELD = NEW_LINE;
static final char[] HEXADECIMAL = "0123456789ABCDEF".toCharArray();
static final Pattern REGX_PATTERN = Pattern.compile("\\.|\\$");
static final Supplier<StopCharTester> QUOTES_ESCAPING = StopCharTesters.QUOTES::escaping;
static final Supplier<StopCharTester> SINGLE_QUOTES_ESCAPING = StopCharTesters.SINGLE_QUOTES::escaping;
static final Supplier<StopCharTester> END_OF_TEXT_ESCAPING = TextStopCharTesters.END_OF_TEXT::escaping;
static final Supplier<StopCharsTester> STRICT_END_OF_TEXT_ESCAPING = TextStopCharsTesters.STRICT_END_OF_TEXT::escaping;
static final Supplier<StopCharsTester> END_EVENT_NAME_ESCAPING = TextStopCharsTesters.END_EVENT_NAME::escaping;

static {
IOTools.unmonitor(TYPE);
Expand Down Expand Up @@ -204,8 +209,7 @@ public static <ACS extends Appendable & CharSequence> void unescape(@NotNull ACS

@Nullable
static StopCharTester getEscapingSingleQuotes() {
StopCharTester sct = ThreadLocalHelper.getTL(ESCAPED_SINGLE_QUOTES,
StopCharTesters.SINGLE_QUOTES::escaping);
StopCharTester sct = ThreadLocalHelper.getTL(ESCAPED_SINGLE_QUOTES, SINGLE_QUOTES_ESCAPING);
// reset it.
sct.isStopChar(' ');
return sct;
Expand Down Expand Up @@ -539,41 +543,43 @@ private <K> K toExpected(Class<K> expectedClass, StringBuilder sb) {

@NotNull
protected StopCharTester getEscapingEndOfText() {
StopCharTester escaping = ThreadLocalHelper.getTL(ESCAPED_END_OF_TEXT,
TextStopCharTesters.END_OF_TEXT::escaping);
StopCharTester escaping = ThreadLocalHelper.getTL(ESCAPED_END_OF_TEXT, END_OF_TEXT_ESCAPING);
// reset it.
escaping.isStopChar(' ');
return escaping;
}

@NotNull
protected StopCharsTester getStrictEscapingEndOfText() {
TextStopCharsTesters strictEndOfText = strictEndOfText();
StopCharsTester escaping = ThreadLocalHelper.getTL(STRICT_ESCAPED_END_OF_TEXT,
strictEndOfText::escaping);
StopCharsTester escaping = ThreadLocalHelper.getTL(STRICT_ESCAPED_END_OF_TEXT, strictEndOfTextEscaping());
// reset it.
escaping.isStopChar(' ', ' ');
return escaping;
}

@NotNull
@Deprecated(/* To be removed in 2.24 - use strictEndOfTextEscaping */)
protected TextStopCharsTesters strictEndOfText() {
return TextStopCharsTesters.STRICT_END_OF_TEXT;
}

@NotNull
protected Supplier<StopCharsTester> strictEndOfTextEscaping() {
return strictEndOfText() == TextStopCharsTesters.STRICT_END_OF_TEXT ?
STRICT_END_OF_TEXT_ESCAPING : strictEndOfText()::escaping;
}

@NotNull
protected StopCharsTester getEscapingEndEventName() {
StopCharsTester escaping = ThreadLocalHelper.getTL(STRICT_ESCAPED_END_OF_TEXT,
TextStopCharsTesters.END_EVENT_NAME::escaping);
StopCharsTester escaping = ThreadLocalHelper.getTL(STRICT_ESCAPED_END_OF_TEXT, END_EVENT_NAME_ESCAPING);
// reset it.
escaping.isStopChar(' ', ' ');
return escaping;
}

@Nullable
protected StopCharTester getEscapingQuotes() {
StopCharTester sct = ThreadLocalHelper.getTL(ESCAPED_QUOTES,
StopCharTesters.QUOTES::escaping);
StopCharTester sct = ThreadLocalHelper.getTL(ESCAPED_QUOTES, QUOTES_ESCAPING);
// reset it.
sct.isStopChar(' ');
return sct;
Expand Down

0 comments on commit 308f2dd

Please sign in to comment.