Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active November 28, 2020 07:55
Show Gist options
  • Save turboBasic/4170a84b68d1b388aa6fda40086112ca to your computer and use it in GitHub Desktop.
Save turboBasic/4170a84b68d1b388aa6fda40086112ca to your computer and use it in GitHub Desktop.
Groovy String split() vs tokenize() #groovy
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