Last active
November 28, 2020 07:55
-
-
Save turboBasic/4170a84b68d1b388aa6fda40086112ca to your computer and use it in GitHub Desktop.
Groovy String split() vs tokenize() #groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final List TestCases = [ | |
'/usr/bin/sh', | |
'//usr/bin/sh', | |
'usr//bin/sh', | |
'usr//bin//sh///', | |
'//', | |
'/', | |
' / ', | |
' // ', | |
' ', | |
'', | |
null, | |
[a: '/'], | |
] | |
final String RESULT = '''\ | |
str split'/' .tokenize'/' | |
/usr/bin/sh [, usr, bin, sh] [usr, bin, sh] | |
//usr/bin/sh [, , usr, bin, sh] [usr, bin, sh] | |
usr//bin/sh [usr, , bin, sh] [usr, bin, sh] | |
usr//bin//sh/// [usr, , bin, , sh] [usr, bin, sh] | |
// [] [] | |
/ [] [] | |
/ [ , ] [ , ] | |
// [ , , ] [ , ] | |
[ ] [ ] | |
[] [] | |
null null null | |
[a:/] null null | |
''' | |
final List HEADER = [ 'str', $/split'/'/$, $/.tokenize'/'/$ ] | |
println toPrintedString(*HEADER) | |
println() | |
TestCases.each { | |
List s, t | |
try { s = it.split'/' } catch(e) {} | |
try { t = it.tokenize'/' } catch(e) {} | |
println toPrintedString( it, s, t ) | |
} | |
String toPrintedString(Object[] args) { | |
args | |
.inject(''<<'') { | |
acc, val -> acc << val.toString().padRight(25) | |
} | |
.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment