aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
diff options
context:
space:
mode:
authorGravatar Jon Brandvein <brandjon@google.com>2017-01-02 18:43:42 +0000
committerGravatar John Cater <jcater@google.com>2017-01-03 15:04:36 +0000
commit36ecf16f770ce2faaa49cf488c427968a7b29631 (patch)
treeca6de66434e25547891d426b67d9879612887165 /src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
parentd810d9ad51aca1d8b7e74406cb80943791f90547 (diff)
Make the string strip() methods compatible with Python
strip(), lstrip(), and rstrip() now accept a None argument as a synonym for the no-arg version. The characters that are considered as whitespace (to be removed by default in the no-arg form) are now the same as in Python 3.6. RELNOTES[INC]: The string methods strip(), lstrip(), and rstrip() now by default remove the same whitespace characters as Python 3 does, and accept None as an argument. -- PiperOrigin-RevId: 143388250 MOS_MIGRATED_REVID=143388250
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java59
1 files changed, 44 insertions, 15 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
index 572b281584..adb7b2902c 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
@@ -102,6 +102,29 @@ public class MethodLibrary {
}
};
+ /**
+ * For consistency with Python we recognize the same whitespace characters as they do over the
+ * range 0x00-0xFF. See https://hg.python.org/cpython/file/3.6/Objects/unicodetype_db.h#l5738
+ * This list is a consequence of Unicode character information.
+ *
+ * Note that this differs from Python 2.7, which uses ctype.h#isspace(), and from
+ * java.lang.Character#isWhitespace(), which does not recognize U+00A0.
+ */
+ private static final String LATIN1_WHITESPACE = (
+ "\u0009"
+ + "\n"
+ + "\u000B"
+ + "\u000C"
+ + "\r"
+ + "\u001C"
+ + "\u001D"
+ + "\u001E"
+ + "\u001F"
+ + "\u0020"
+ + "\u0085"
+ + "\u00A0"
+ );
+
private static String stringLStrip(String self, String chars) {
CharMatcher matcher = CharMatcher.anyOf(chars);
for (int i = 0; i < self.length(); i++) {
@@ -137,14 +160,16 @@ public class MethodLibrary {
@Param(
name = "chars",
type = String.class,
- doc = "The characters to remove",
- defaultValue = "' \\t\\n\\r'" // \f \v are illegal in Skylark
+ noneable = true,
+ doc = "The characters to remove, or all whitespace if None",
+ defaultValue = "None"
)
}
)
private static final BuiltinFunction lstrip =
new BuiltinFunction("lstrip") {
- public String invoke(String self, String chars) {
+ public String invoke(String self, Object charsOrNone) {
+ String chars = charsOrNone != Runtime.NONE ? (String) charsOrNone : LATIN1_WHITESPACE;
return stringLStrip(self, chars);
}
};
@@ -162,16 +187,18 @@ public class MethodLibrary {
parameters = {
@Param(name = "self", type = String.class, doc = "This string"),
@Param(
- name = "chars",
- type = String.class,
- doc = "The characters to remove",
- defaultValue = "' \\t\\n\\r'" // \f \v are illegal in Skylark
- )
+ name = "chars",
+ type = String.class,
+ noneable = true,
+ doc = "The characters to remove, or all whitespace if None",
+ defaultValue = "None"
+ )
}
)
private static final BuiltinFunction rstrip =
new BuiltinFunction("rstrip") {
- public String invoke(String self, String chars) {
+ public String invoke(String self, Object charsOrNone) {
+ String chars = charsOrNone != Runtime.NONE ? (String) charsOrNone : LATIN1_WHITESPACE;
return stringRStrip(self, chars);
}
};
@@ -189,16 +216,18 @@ public class MethodLibrary {
parameters = {
@Param(name = "self", type = String.class, doc = "This string"),
@Param(
- name = "chars",
- type = String.class,
- doc = "The characters to remove",
- defaultValue = "' \\t\\n\\r'" // \f \v are illegal in Skylark
- )
+ name = "chars",
+ type = String.class,
+ noneable = true,
+ doc = "The characters to remove, or all whitespace if None",
+ defaultValue = "None"
+ )
}
)
private static final BuiltinFunction strip =
new BuiltinFunction("strip") {
- public String invoke(String self, String chars) {
+ public String invoke(String self, Object charsOrNone) {
+ String chars = charsOrNone != Runtime.NONE ? (String) charsOrNone : LATIN1_WHITESPACE;
return stringLStrip(stringRStrip(self, chars), chars);
}
};