diff --git a/quarkc/lib/quark.q b/quarkc/lib/quark.q index f4997429..a8b8703d 100644 --- a/quarkc/lib/quark.q +++ b/quarkc/lib/quark.q @@ -1364,7 +1364,15 @@ namespace quark { bool __eq__(String other); int size(); String substring(int start, int end); - + String strip(); + bool startsWith(String other); + bool endsWith(String other); + int find(String other); + String replaceAll(String from, String to); + String join(List parts); + List split(String sep); + String toUpper(); + String toLower(); String __add__(String other) for java { return $self + $other; } bool __eq__(String other) for java { return $self.equals($other); } @@ -1373,6 +1381,32 @@ namespace quark { int l = $self.length(); return $self.substring($start, $end < l ? $end : l); } + String strip() for java { return ($self).trim(); } + bool startsWith(String other) for java { return Boolean.valueOf(($self).startsWith($other)); } + bool endsWith(String other) for java { return Boolean.valueOf(($self).endsWith($other)); } + int find(String other) for java { return ($self).indexOf($other); } + String replaceAll(String from, String to) for java import "java.util.regex.Pattern" { + return ($self).replaceAll(Pattern.quote($from), ($to)); + } + List split(String sep) for java import "java.util.ArrayList" + import "java.util.Arrays" + import "java.util.regex.Pattern" { + return new ArrayList(Arrays.asList(($self).split(Pattern.quote($sep), -1))); } + String join(List parts) for java { + StringBuilder b = new StringBuilder(); + boolean first = true; + for (String part : $parts) { + if (first) { + first = false; + } else { + b.append($self); + } + b.append(part); + } + return b.toString(); + } + String toUpper() for java { return ($self).toUpperCase(); } + String toLower() for java { return ($self).toLowerCase(); } String __add__(String other) for go { return $self + $other } @@ -1393,11 +1427,32 @@ namespace quark { } return $self[s:e] } + String strip() for go import "strings" { return strings.TrimSpace($self) } + bool startsWith(String other) for go import "strings" { return strings.HasPrefix(($self), ($other)) } + bool endsWith(String other) for go import "strings" { return strings.HasSuffix(($self), ($other)) } + int find(String other) for go import "strings" { return strings.Index($self, $other) } + String replaceAll(String from, String to) for go import "strings" { return strings.Replace($self, $from, $to, -1) } + List split(String sep) for go import "strings" { + l := strings.Split($self, $sep) + return &l + } + String join(List parts) for go import "strings" { return strings.Join(*($parts), $self)} + String toUpper() for go import "strings"{ return strings.ToUpper($self) } + String toLower() for go import "strings" { return strings.ToLower($self) } String __add__(String other) for python { return $self + $other } bool __eq__(String other) for python { return $self == $other } int size() for python { return len($self) } String substring(int start, int end) for python { return $self[$start:$end] } + String strip() for python { return ($self).strip() } + bool startsWith(String other) for python { return ($self).startswith($other) } + bool endsWith(String other) for python { return ($self).endswith($other) } + int find(String other) for python { return ($self).find($other) } + String replaceAll(String from_, String to) for python { return ($self).replace(($from_), ($to)) } + List split(String sep) for python { return ($self).split($sep) } + String join(List parts) for python { return ($self).join($parts) } + String toUpper() for python { return ($self).upper() } + String toLower() for python { return ($self).lower() } String __add__(String other) for ruby { return $self + $other } bool __eq__(String other) for ruby { return $self == $other } @@ -1405,11 +1460,34 @@ namespace quark { String substring(int start, int end) for ruby { return $self.slice($start, $end - $start) } + String strip() for ruby { return ($self).strip } + bool startsWith(String other) for ruby { return ($self).start_with?($other) } + bool endsWith(String other) for ruby { return ($self).end_with?($other) } + int find(String other) for ruby { return (($self).index($other) or -1) } + String replaceAll(String from, String to) for ruby { return ($self).gsub(($from), ($to)) } + List split(String sep) for ruby { + return ['', ''] if $self == $sep + result = $self.split($sep) + result = result + [''] if ($self).end_with? $sep + result + } + String join(List parts) for ruby { return ($parts).join($self) } + String toUpper() for ruby { return ($self).upcase } + String toLower() for ruby { return ($self).downcase } String __add__(String other) for javascript { return $self + $other } bool __eq__(String other) for javascript { return $self === $other } int size() for javascript { return $self.length } String substring(int start, int end) for javascript { return $self.substring($start, $end) } + String strip() for javascript { return ($self).trim() } + bool startsWith(String other) for javascript { return (($self).indexOf($other)===0)} + bool endsWith(String other) for javascript { return (($self).indexOf(($other), ($self).length - ($other).length) !== -1)} + int find(String other) for javascript { return ($self).indexOf($other)} + String replaceAll(String from, String to) for javascript { return ($self).split($from).join($to) } + List split(String sep) for javascript { return ($self).split($sep)} + String join(List parts) for javascript { return ($parts).join($self)} + String toUpper() for javascript { return ($self).toUpperCase()} + String toLower() for javascript { return ($self).toLowerCase()} Any to_quark_Any(); Scalar to_quark_Scalar(); diff --git a/quarkc/test/e2e/primitive_strings.q b/quarkc/test/e2e/primitive_strings.q new file mode 100644 index 00000000..579ad9d4 --- /dev/null +++ b/quarkc/test/e2e/primitive_strings.q @@ -0,0 +1,113 @@ +class StringTest { + void concat() { + assertEqual(true, "ab" == "a" + "b"); + assertEqual(true, "ab" == "ab" + ""); + assertEqual(true, "ab" == "" + "ab"); + + assertEqual(true, "a\nb" == "a\nb" + ""); + assertEqual(true, "a\nb" == "a\n" + "b"); + assertEqual(true, "a\nb" == "a" + "\nb"); + assertEqual(true, "a\nb" == "" + "a\nb"); + + assertEqual(true, "ab\n" == "ab\n" + ""); + assertEqual(true, "ab\n" == "ab" + "\n"); + assertEqual(true, "ab\n" == "a" + "b\n"); + assertEqual(true, "ab\n" == "" + "ab\n"); + + assertEqual(true, "a" == "\x61"); + assertEqual(true, "\x0a" == "\n"); + } + + void substring() { + assertEqual(true, "abc" == "abcdefghi".substring(0, 3)); + assertEqual(true, "def" == "abcdefghi".substring(3, 6)); + assertEqual(true, "ghi" == "abcdefghi".substring(6, 9)); + } + + void equals() { + assertEqual(true, ("abc" == "abc")); + assertEqual(false, ("abc" == "def")); + } + + void upper_lower() { + assertEqual(true, "asdf" == "AsDf".toLower()); + assertEqual(true, "ASDF" == "AsDf".toUpper()); + } + + void strip() { + assertEqual(true, "Abc".strip() == "Abc"); + assertEqual(true, "Abc ".strip() == "Abc"); + assertEqual(true, " Abc".strip() == "Abc"); + assertEqual(true, " Abc ".strip() == "Abc"); + assertEqual(true, "\t \nAbc\n \t ".strip() == "Abc"); + + List ws = ["", " ", "\t", "\n", "\r"]; + String s = "<--->"; + int i = 0; + while (i < ws.size()) { + int j = 0; + while (j < ws.size()) { + assertEqual(true, s == (ws[i] + s + ws[j]).strip()); + j = j + 1; + } + assertEqual(true, "" == ws[i].strip()); + i = i + 1; + } + } + + void join() { + assertEqual(true, "".join([]) == ""); + assertEqual(true, "++".join([]) == ""); + assertEqual(true, "".join(["a", "bc"]) == "abc"); + assertEqual(true, "++".join(["a", "bc", "d"]) == "a++bc++d"); + } + + void split() { + List l = ["a", "c"]; + assertEqual(true, "abc".split("b") == l); + l = ["a", "c", "", "d"]; + assertEqual(true, "aGOcGOGOd".split("GO") == l); + l = ["abc"]; + assertEqual(true, "abc".split("z") == l); + } + + void replaceAll() { + assertEqual(true, "abc".replaceAll("bc", "z") == "az"); + assertEqual(true, "aGOdGO".replaceAll("GO", "me") == "amedme"); + // Make sure we're not somehow using regexs: + assertEqual(true, "a.csc".replaceAll(".c", "z") == "azsc"); + } + + void endsWith() { + assertEqual(true, "abc".endsWith("") == true); + assertEqual(true, "abc".endsWith("c") == true); + assertEqual(true, "abc".endsWith("bc") == true); + assertEqual(true, "abc".endsWith("abc") == true); + assertEqual(true, "abc".endsWith("abcd") == false); + assertEqual(true, "abc".endsWith("ab") == false); + assertEqual(true, "abc".endsWith("z") == false); + } + + void startsWith() { + assertEqual(true, "abc".startsWith("") == true); + assertEqual(true, "abc".startsWith("a") == true); + assertEqual(true, "abc".startsWith("ab") == true); + assertEqual(true, "abc".startsWith("abc") == true); + assertEqual(true, "abc".startsWith("abcd") == false); + assertEqual(true, "abc".startsWith("bc") == false); + assertEqual(true, "abc".startsWith("z") == false); + } + + void find() { + assertEqual(true, "Abc".find("z") == -1); + assertEqual(true, "Abc".find("") == 0); + assertEqual(true, "Abc".find("A") == 0); + assertEqual(true, "Abc".find("Ab") == 0); + assertEqual(true, "Abc".find("Abc") == 0); + assertEqual(true, "Abc".find("Abcz") == -1); + assertEqual(true, "Abc".find("zAbc") == -1); + assertEqual(true, "Abc".find("b") == 1); + assertEqual(true, "Abc".find("bc") == 1); + assertEqual(true, "Abc".find("c") == 2); + } +} diff --git a/quarkc/test/e2e/primitives.q b/quarkc/test/e2e/primitives.q index 23bfcc3d..81293f56 100644 --- a/quarkc/test/e2e/primitives.q +++ b/quarkc/test/e2e/primitives.q @@ -346,41 +346,6 @@ class FloatTest { } } - -class StringTest { - void concat() { - assertEqual("ab", "a" + "b"); - assertEqual("ab", "ab" + ""); - assertEqual("ab", "" + "ab"); - - assertEqual("a\nb", "a\nb" + ""); - assertEqual("a\nb", "a\n" + "b"); - assertEqual("a\nb", "a" + "\nb"); - assertEqual("a\nb", "" + "a\nb"); - - assertEqual("ab\n", "ab\n" + ""); - assertEqual("ab\n", "ab" + "\n"); - assertEqual("ab\n", "a" + "b\n"); - assertEqual("ab\n", "" + "ab\n"); - - assertEqual("a", "\x61"); - assertEqual("\x0a", "\n"); - } - - void substring() { - assertEqual("abc", "abcdefghi".substring(0, 3)); - assertEqual("def", "abcdefghi".substring(3, 6)); - assertEqual("ghi", "abcdefghi".substring(6, 9)); - } - - void equals() { - assertEqual(true, "abc" == "abc"); - assertEqual(false, "abc" == "def"); - } - -} - - Any weirdo(); Any weirdo() for java { return new Object(); } Any weirdo() for go { return make(chan int, 100) }