diff options
author | 2015-06-05 22:13:21 +0000 | |
---|---|---|
committer | 2015-06-08 12:54:00 +0000 | |
commit | c3ad541c80dfdfaeedabcd3919e779c24138c2c8 (patch) | |
tree | 819dd8025ea733c06dc38c6e8fed59fcf8663329 /src | |
parent | 37e59a778dc385748e8b5e644cf4abc5b7877412 (diff) |
Skylark: fix escaping of backslash
Create a Printer class into which to eventually move all printing infrastructure
currently in EvalUtils. For now, only move string escaping there.
Fix the forgotten case of backslash.
Allow any style of Python quotes, while we're at it,
but keep using simple double-quote as the default.
--
MOS_MIGRATED_REVID=95328052
Diffstat (limited to 'src')
3 files changed, 88 insertions, 26 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java index c3e23f3959..3468566e79 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java @@ -460,32 +460,7 @@ public abstract class EvalUtils { o = o.toString(); // Pretty-print a label like a string } if (o instanceof String) { - String s = (String) o; - buffer.append('"'); - for (int ii = 0, len = s.length(); ii < len; ++ii) { - char c = s.charAt(ii); - switch (c) { - case '\r': - buffer.append('\\').append('r'); - break; - case '\n': - buffer.append('\\').append('n'); - break; - case '\t': - buffer.append('\\').append('t'); - break; - case '\"': - buffer.append('\\').append('"'); - break; - default: - if (c < 32) { - buffer.append(String.format("\\x%02x", (int) c)); - } else { - buffer.append(c); // no need to support UTF-8 - } - } // endswitch - } - buffer.append('\"'); + Printer.writeString(buffer, (String) o); } else { printValueX(o, buffer); } diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java new file mode 100644 index 0000000000..259b34bb9d --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java @@ -0,0 +1,83 @@ +// Copyright 2015 Google Inc. 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License 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. +package com.google.devtools.build.lib.syntax; + +import java.io.IOException; + +/** + * (Pretty) Printing of Skylark values + */ +public final class Printer { + + private Printer() { + } + + private static Appendable backslashChar(Appendable buffer, char c) throws IOException { + return buffer.append('\\').append(c); + } + + private static Appendable escapeCharacter(Appendable buffer, char c, char quote) + throws IOException { + if (c == quote) { + return backslashChar(buffer, c); + } + switch (c) { + case '\\': + return backslashChar(buffer, '\\'); + case '\r': + return backslashChar(buffer, 'r'); + case '\n': + return backslashChar(buffer, 'n'); + case '\t': + return backslashChar(buffer, 't'); + default: + if (c < 32) { + return buffer.append(String.format("\\x%02x", (int) c)); + } + return buffer.append(c); // no need to support UTF-8 + } // endswitch + } + + /** + * Write a properly escaped Skylark representation of a string to a buffer. + * + * @param buffer the Appendable we're writing to. + * @param s the string a representation of which to write. + * @param quote the quote character to use, '"' or '\''. + * @return the Appendable, in fluent style. + */ + public static Appendable writeString(Appendable buffer, String s, char quote) + throws IOException { + buffer.append(quote); + int len = s.length(); + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + escapeCharacter(buffer, c, quote); + } + return buffer.append(quote); + } + + /** + * Write a properly escaped Skylark representation of a string to a buffer. + * Use standard Skylark convention, i.e., double-quoted single-line string, + * as opposed to standard Python convention, i.e. single-quoted single-line string. + * + * @param buffer the Appendable we're writing to. + * @param s the string a representation of which to write. + * @return the buffer, in fluent style. + */ + public static Appendable writeString(Appendable buffer, String s) throws IOException { + return writeString(buffer, s, '"'); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java b/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java index e4a06e5ef2..13c0987364 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java @@ -85,12 +85,16 @@ public class EvalUtilsTest { // labels and strings at toplevel. assertEquals("foo\nbar", EvalUtils.printValue("foo\nbar")); assertEquals("\"foo\\nbar\"", EvalUtils.prettyPrintValue("foo\nbar")); + assertEquals("foo\nbar", EvalUtils.printValue("foo\nbar")); assertEquals("'", EvalUtils.printValue("'")); assertEquals("\"'\"", EvalUtils.prettyPrintValue("'")); assertEquals("\"", EvalUtils.printValue("\"")); assertEquals("\"\\\"\"", EvalUtils.prettyPrintValue("\"")); + assertEquals("a\\b", EvalUtils.printValue("a\\b")); + assertEquals("\"a\\\\b\"", EvalUtils.prettyPrintValue("a\\b")); assertEquals("3", EvalUtils.printValue(3)); assertEquals("3", EvalUtils.prettyPrintValue(3)); + assertEquals("None", EvalUtils.printValue(Environment.NONE)); assertEquals("None", EvalUtils.prettyPrintValue(Environment.NONE)); assertEquals("//x:x", EvalUtils.printValue(Label.parseAbsolute("//x"))); |