diff options
author | 2015-12-10 23:30:23 +0000 | |
---|---|---|
committer | 2015-12-11 17:08:32 +0000 | |
commit | cb8f278f42f3c8c7c69314c8cf68175718298216 (patch) | |
tree | 8f0f1cc806c15a1c2520c248f031794a640df0f5 /src/main | |
parent | cb237316c3b5deb1f4c58541ce736a3ed8dde693 (diff) |
Skylark: implemented str.splitlines()
--
MOS_MIGRATED_REVID=109942021
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java | 35 |
1 files changed, 35 insertions, 0 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 43939049d3..2f17084a50 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 @@ -636,6 +636,41 @@ public class MethodLibrary { } }; + @SkylarkSignature(name = "splitlines", objectType = StringModule.class, + returnType = MutableList.class, + doc = + "Splits the string at line boundaries ('\\n', '\\r\\n', '\\r') " + + "and returns the result as a list.", + mandatoryPositionals = { + @Param(name = "self", type = String.class, doc = "This string.")}, + optionalPositionals = { + @Param(name = "keepends", type = Boolean.class, defaultValue = "False", + doc = "Whether the line breaks should be included in the resulting list.")}) + private static BuiltinFunction splitLines = new BuiltinFunction("splitlines") { + @SuppressWarnings("unused") + public MutableList invoke(String self, Boolean keepEnds) throws EvalException { + List<String> result = new ArrayList<>(); + Matcher matcher = SPLIT_LINES_PATTERN.matcher(self); + while (matcher.find()) { + String line = matcher.group("line"); + String lineBreak = matcher.group("break"); + boolean trailingBreak = lineBreak.isEmpty(); + if (line.isEmpty() && trailingBreak) { + break; + } + if (keepEnds && !trailingBreak) { + result.add(line + lineBreak); + } else { + result.add(line); + } + } + return new MutableList(result); + } + }; + + private static final Pattern SPLIT_LINES_PATTERN = + Pattern.compile("(?<line>.*)(?<break>(\\r\\n|\\r|\\n)?)"); + @SkylarkSignature(name = "isalpha", objectType = StringModule.class, returnType = Boolean.class, doc = "Returns True if all characters in the string are alphabetic ([a-zA-Z]) and it " + "contains at least one character.", |