digitalmars.D - nonallocating unicode string manipulations
- Timothee Cour (48/48) Jul 16 2013 phobos is lacking nonallocating string manipulation functions.
phobos is lacking nonallocating string manipulation functions.
I made these inplace string=3D>string functions (see unittests):
----
auto takeInplace(T)(T a,size_t n)if(is(T=3D=3Dstring));
auto slice(T)(T a,size_t u, size_t v)if(is(T=3D=3Dstring));
unittest{
auto a=3D"=E2=89=88a=C3=A7=C3=A7=E2=88=9Aef";
auto b=3Da.takeInplace(3);
assert(b=3D=3D"=E2=89=88a=C3=A7");
assert(a.ptr=3D=3Db.ptr);
assert(a.takeInplace(10)=3D=3Da);
}
unittest{
import std.range;
auto a=3D"=E2=89=88a=C3=A7=C3=A7=E2=88=9Aef";
auto b=3Da.slice(2,6);
assert(a.slice(2,6)=3D=3D"=C3=A7=C3=A7=E2=88=9Ae");
assert(a.slice(2,6).ptr=3D=3Da.slice(2,3).ptr);
assert(a.slice(0,a.walkLength) is a);
import std.exception;
assertThrown(a.slice(2,8));
assertThrown(a.slice(2,1));
}
----
A)
would they belong in phobos? which module?
B)
I'd also like to have an efficient range interface over strings that has
reference semantics on 'front' property:
----
auto a=3D"=E2=89=88a=C3=A7=C3=A7=E2=88=9Aef";
foreach(i, ref ai; a.byElement){
alias T=3Dtypeof(a); //eg:T=3D=3Dchar[]
alias E=3DForEachType!T; //eg: E=3D=3Dchar
assert(is(typeof(ai) =3D=3D T)); //ai is same type as a; it's a slice int=
o it
if(i=3D=3D0)
assert(ai.ptr =3D=3D a.ptr);
}
----
This would make it easy for example to implement other non-allocating
inplace unicode functions, such as: toUpper:
auto toUpper(T)(T a){
foreach(i, ref ai; a.byElement){
ai[]=3Dai.toUpper.to!(typeof(ai)); //throws if some rare character has it's
toUpper of different size as its lowercase.
}
}
Jul 16 2013








Timothee Cour <thelastmammoth gmail.com>