From bcc6b7e46ceae4ced0d75bf3f7d491325c517387 Mon Sep 17 00:00:00 2001 From: Radhika Date: Fri, 16 Oct 2026 22:18:21 +0530 Subject: [PATCH] Quote operator symbols containing // or /* in the text writer A symbol made only of operator characters is written unquoted inside an s-expression, but // and /* start a comment when the text is read back, so the symbol and whatever follows it on re-read are lost or reparsed. --- .../ion/impl/_Private_IonTextAppender.java | 24 +++++------- .../com/amazon/ion/util/IonTextUtils.java | 24 +++++------- .../com/amazon/ion/impl/TextWriterTest.java | 39 ++++++++++++------- .../java/com/amazon/ion/util/TextTest.java | 26 ++++++------- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/_Private_IonTextAppender.java b/src/main/java/com/amazon/ion/impl/_Private_IonTextAppender.java index e74b503bf5..afcecda616 100644 --- a/src/main/java/com/amazon/ion/impl/_Private_IonTextAppender.java +++ b/src/main/java/com/amazon/ion/impl/_Private_IonTextAppender.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; import static com.amazon.ion.impl._Private_IonConstants.MAX_LONG_TEXT_SIZE; @@ -507,6 +494,13 @@ public static boolean symbolNeedsQuoting(CharSequence symbol, if (!isOperatorPart(c)) { return true; } + // "//" and "/*" start a comment when read back. + if (c == '/' && ii + 1 < length) { + char next = symbol.charAt(ii + 1); + if (next == '/' || next == '*') { + return true; + } + } } return false; } diff --git a/src/main/java/com/amazon/ion/util/IonTextUtils.java b/src/main/java/com/amazon/ion/util/IonTextUtils.java index 089aa4a89a..0a9faaada2 100644 --- a/src/main/java/com/amazon/ion/util/IonTextUtils.java +++ b/src/main/java/com/amazon/ion/util/IonTextUtils.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.util; import static com.amazon.ion.impl._Private_IonConstants.isHighSurrogate; @@ -201,6 +188,13 @@ public static SymbolVariant symbolVariant(CharSequence symbol) if (!isOperatorPart(c)) { return SymbolVariant.QUOTED; } + // "//" and "/*" start a comment when read back. + if (c == '/' && ii + 1 < length) { + char next = symbol.charAt(ii + 1); + if (next == '/' || next == '*') { + return SymbolVariant.QUOTED; + } + } } return SymbolVariant.OPERATOR; } diff --git a/src/test/java/com/amazon/ion/impl/TextWriterTest.java b/src/test/java/com/amazon/ion/impl/TextWriterTest.java index 3090dc73fb..29da60714f 100644 --- a/src/test/java/com/amazon/ion/impl/TextWriterTest.java +++ b/src/test/java/com/amazon/ion/impl/TextWriterTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; import static com.amazon.ion.SystemSymbols.ION_1_0; @@ -26,6 +13,7 @@ import com.amazon.ion.IonReader; import com.amazon.ion.IonSequence; import com.amazon.ion.IonStruct; +import com.amazon.ion.IonType; import com.amazon.ion.IonWriter; import com.amazon.ion.SymbolTable; import com.amazon.ion.SystemSymbols; @@ -540,6 +528,27 @@ public void testWritingFloatNegativeInfinityToJson() assertEquals("null", actual); } + @Test + public void testOperatorSymbolThatStartsCommentIsQuoted() + throws Exception + { + iw = makeWriter(); + iw.stepIn(IonType.SEXP); + iw.writeSymbol("/"); + iw.writeSymbol("//"); + iw.writeSymbol("+/*"); + iw.writeSymbol("x"); + iw.stepOut(); + iw.writeSymbol("y"); + + String actual = outputString(); + assertEquals("(/ '//' '+/*' x) y", actual); + + IonDatagram dg = system().getLoader().load(actual); + assertEquals(2, dg.size()); + assertEquals(4, ((IonSequence) dg.get(0)).size()); + } + @Test public void testSuppressInitialIvm() throws Exception diff --git a/src/test/java/com/amazon/ion/util/TextTest.java b/src/test/java/com/amazon/ion/util/TextTest.java index 6f34e0f630..ab231b452a 100644 --- a/src/test/java/com/amazon/ion/util/TextTest.java +++ b/src/test/java/com/amazon/ion/util/TextTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.util; import com.amazon.ion.BlobTest; @@ -61,6 +48,15 @@ public void testSymbolNeedsQuoting() unquotedInSexp("!"); unquotedInSexp("<"); unquotedInSexp("<==="); + unquotedInSexp("/"); + unquotedInSexp("*/"); + + // These would start a comment if left unquoted. + quotedEverywhere("//"); + quotedEverywhere("/*"); + quotedEverywhere("+//"); + quotedEverywhere("+/*"); + quotedEverywhere("*//"); quotedEverywhere("");