improv strip,chomp,chug with trailling pool of character
it would be nice to be able to strip characters other than ISSPACE in the strip/chug/chomp functions, for example to strip quotes or parentheses.
[CCode (cname = "g_strchomp")]
public unowned string _chomp();
public string chomp (string? characters = null) {
unichar t;
int end;
if (characters == null) {
var result = this.dup().__strip_chomp ();
return result;
}
end = this.length;
while (end > 0) {
t = utf8_get_char ((char*)this + end);
if (utf8_strchr((char*)characters, -1, t) == null) {
this.get_next_char(ref end, out t);
break;
}
end = (int)(utf8_prev_char((char*)this + end) - (char*)this);
}
return this[0:end];
}
[CCode (cname = "g_strchug")]
public unowned string _chug();
public string chug (string? characters = null) {
unichar t;
int start = 0;
if (characters == null) {
var result = this.dup()._chug ();
return result;
}
while ((t = utf8_get_char ((char*)this + start)) != '\0') {
if (utf8_strchr((char*)characters, -1, t) == null)
break;
start = (int)(utf8_next_char((char*)this + start) - (char*)this);
}
return this[start:];
}
[CCode (cname = "g_strstrip")]
public unowned string _strip ();
public string strip (string? characters = null) {
int end;
int start;
unichar t;
if (characters == null) {
var result = this.dup()._strip ();
return result;
}
start = 0;
end = this.length;
while ((t = utf8_get_char ((char*)this + start)) != '\0') {
if (utf8_strchr((char*)characters, -1, t) == null)
break;
start = (int)(utf8_next_char((char*)this + start) - (char*)this);
}
while (end > 0) {
t = utf8_get_char ((char*)this + end);
if (utf8_strchr((char*)characters, -1, t) == null) {
this.get_next_char(ref end, out t);
break;
}
end = (int)(utf8_prev_char((char*)this + end) - (char*)this);
}
return this[start:end];
}
this code support UTF-8 character
this test work:
void main()
{
var str = "éé((({{{'''' <Hello World> ''''''}}})))";
str = strip("é{}([' '])");
print (str);
//output <Hello World>
}
functions will keep their default operation and will not break any compatibility
Edited by Hydral Nathan Jordan