feat: add trim, trimEnd, trimStart methods with optional chars argument to String class
Implement six new trimming methods on String: trim(), trim(chars), trimEnd(), trimEnd(chars), trimStart(), trimStart(chars). The core logic uses a private trim_ helper that accepts a chars string, converts it to code points, and iterates from the start and/or end of the string to remove matching characters. Default whitespace set is tab, carriage return, newline, and space. Includes comprehensive test coverage for default and custom character trimming, edge cases (empty strings, all-trimmed strings, 8-bit clean data), and runtime error when chars argument is not a string.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
System.print("".trim() == "") // expect: true
|
||||
System.print("foo".trim() == "foo") // expect: true
|
||||
System.print(" \t\r\nfoo b\tar \t\r\n".trim() == "foo b\tar") // expect: true
|
||||
System.print(" \t\r\n \t\r\n".trim() == "") // expect: true
|
||||
System.print(" \n\n\tsøméஃthîng \n\n\t".trim() == "søméஃthîng") // expect: true
|
||||
|
||||
System.print("".trim("abc") == "") // expect: true
|
||||
System.print("foo".trim("abc") == "foo") // expect: true
|
||||
System.print("foo".trim("") == "foo") // expect: true
|
||||
System.print("cbacbfoobarab".trim("abc") == "foobar") // expect: true
|
||||
System.print("abcbacba".trim("abc") == "") // expect: true
|
||||
System.print("søméஃthîngsøméஃ".trim("ஃmésø") == "thîng") // expect: true
|
||||
|
||||
// 8-bit clean.
|
||||
System.print(" \t\ra\0b \t\r".trim() == "a\0b") // expect: true
|
||||
System.print("\0a\0b\0c\0".trim("c\0a") == "b") // expect: true
|
||||
@@ -0,0 +1 @@
|
||||
"abracadabra".trim(123) // expect runtime error: Characters must be a string.
|
||||
@@ -0,0 +1,16 @@
|
||||
System.print("".trimEnd() == "") // expect: true
|
||||
System.print("foo".trimEnd() == "foo") // expect: true
|
||||
System.print(" \t\r\nfoo b\tar \t\r\n".trimEnd() == " \t\r\nfoo b\tar") // expect: true
|
||||
System.print(" \t\r\n \t\r\n".trimEnd() == "") // expect: true
|
||||
System.print("søméஃthîng \n\n\t".trimEnd() == "søméஃthîng") // expect: true
|
||||
|
||||
System.print("".trimEnd("abc") == "") // expect: true
|
||||
System.print("foo".trimEnd("abc") == "foo") // expect: true
|
||||
System.print("foo".trimEnd("") == "foo") // expect: true
|
||||
System.print("cbacbfoobarab".trimEnd("abc") == "cbacbfoobar") // expect: true
|
||||
System.print("abcbacba".trimEnd("abc") == "") // expect: true
|
||||
System.print("søméஃthîngsøméஃ".trimEnd("ஃmésø") == "søméஃthîng") // expect: true
|
||||
|
||||
// 8-bit clean.
|
||||
System.print(" \t\ra\0b \t\r".trimEnd() == " \t\ra\0b") // expect: true
|
||||
System.print("\0a\0b\0c\0".trimEnd("c\0") == "\0a\0b") // expect: true
|
||||
@@ -0,0 +1 @@
|
||||
"abracadabra".trimEnd(123) // expect runtime error: Characters must be a string.
|
||||
@@ -0,0 +1,16 @@
|
||||
System.print("".trimStart() == "") // expect: true
|
||||
System.print("foo".trimStart() == "foo") // expect: true
|
||||
System.print(" \t\r\nfoo b\tar \t\r\n".trimStart() == "foo b\tar \t\r\n") // expect: true
|
||||
System.print(" \t\r\n \t\r\n".trimStart() == "") // expect: true
|
||||
System.print(" \n\n\tsøméஃthîng".trimStart() == "søméஃthîng") // expect: true
|
||||
|
||||
System.print("".trimStart("abc") == "") // expect: true
|
||||
System.print("foo".trimStart("abc") == "foo") // expect: true
|
||||
System.print("foo".trimStart("") == "foo") // expect: true
|
||||
System.print("cbacbfoobarab".trimStart("abc") == "foobarab") // expect: true
|
||||
System.print("abcbacba".trimStart("abc") == "") // expect: true
|
||||
System.print("søméஃthîng".trimStart("ஃmésø") == "thîng") // expect: true
|
||||
|
||||
// 8-bit clean.
|
||||
System.print(" \t\ra\0b".trimStart() == "a\0b") // expect: true
|
||||
System.print("\0a\0b\0c\0".trimStart("a\0") == "b\0c\0") // expect: true
|
||||
@@ -0,0 +1 @@
|
||||
"abracadabra".trimStart(123) // expect runtime error: Characters must be a string.
|
||||
Reference in New Issue
Block a user