nonallocating unicode string manipulations

Timothee Cour thelastmammoth at gmail.com
Tue Jul 16 21:30:42 PDT 2013


phobos is lacking nonallocating string manipulation functions.

I made these inplace string=>string functions (see unittests):
----
auto takeInplace(T)(T a,size_t n)if(is(T==string));
auto slice(T)(T a,size_t u, size_t v)if(is(T==string));
unittest{
auto a="≈açç√ef";
auto b=a.takeInplace(3);
assert(b=="≈aç");
assert(a.ptr==b.ptr);
assert(a.takeInplace(10)==a);
}
unittest{
import std.range;
auto a="≈açç√ef";
auto b=a.slice(2,6);
assert(a.slice(2,6)=="çç√e");
assert(a.slice(2,6).ptr==a.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="≈açç√ef";
foreach(i, ref ai; a.byElement){
  alias T=typeof(a); //eg:T==char[]
  alias E=ForEachType!T; //eg: E==char
  assert(is(typeof(ai) == T)); //ai is same type as a; it's a slice into it
  if(i==0)
    assert(ai.ptr == 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[]=ai.toUpper.to!(typeof(ai)); //throws if some rare character has it's
toUpper of different size as its lowercase.
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130716/a1e12a88/attachment.html>


More information about the Digitalmars-d mailing list