Nov 27, 2012; 01:00
Jolle Carlestam
Enhancing removetrailing and trim
We had a problem with dirty data sent to us from an external source. Contained name values but with stray chars before and after the names.
String -trim as it is distributed in Lasso 9 does not handle that kind of pressure. Nor does my previous enhancement of string -> trim since it can only handle a single char to trim; string -> trim(',').
In some other languages it is possible to send arrays to trim and have all occurrences of the items in the array trimmed from the string.
We liked that so we've implemented enhancements to make it happen.
Allows calls like this:
local(myvar = ' , |;mystring :, ')
#myvar -> trim(array(`,`, `\s`, `:`, `;`, `|`))
'<pre>'
#myvar
'</pre>'
-> mystring
In order to achieve this we also had to tweak string -> removetrailing. It turns out that string -> removeleading works with regular expressions but string -> removetrailing doesn't. Well, with this addition it does.
This is the code needed to allow trim with arrays:
define string -> removetrailing(find::regexp)=> {
{
.reverse
#find -> input = self
if(!#find -> matchesStart) => {
.reverse
return
}
.remove(1, #find -> matchPosition->second)
.reverse
currentCapture -> restart
}()
}
define string -> trim(trim::array) => {
if(#trim >> `|`) => {
#trim -> removeall(`|`)
#trim -> insert(`\|`)
}
local(remove = `(` + #trim -> join(`|`) + `)`)
.removeleading(regexp(#remove)) & removetrailing(regexp(#remove))
}
We have not done any extensive testing of this yet but it looks promising.
I of course vote for that the above methods or better interpretations of them to be included in future distributions of Lasso.
As should be my previous string -> trim(',') method. If you missed that here's that definition:
define string -> trim(trim::string) => {
.removeleading(#trim) & removetrailing(#trim)
}
HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
the mailing list Lasso
Lasso@lists.lassosoft.com
To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
Send administrative queries to <Lasso-request@lists.lassosoft.com>