<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Oct 26, 2013 at 6:24 PM, Nicolas Sicard <span dir="ltr"><<a href="mailto:dransic@gmail.com" target="_blank">dransic@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sunday, 27 October 2013 at 00:18:41 UTC, Timothee Cour wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've posted a while back a string=>string substring function that doesn't<br>
allocating: google<br>
"nonallocating unicode string manipulations"<br>
<br>
code:<br>
<br>
auto slice(T)(T a,size_t u, size_t v)if(is(T==string)){//TODO:<u></u>generalize to<br>
isSomeString<br>
import std.exception;<br>
auto m=a.length;<br>
size_t i;<br>
enforce(u<=v);<br>
import std.utf;<br>
while(u-- && i<m){<br>
auto si=stride(a,i);<br>
i+=si;<br>
v--;<br>
}<br>
// assert(u==-1);<br>
// enforce(u==-1);<br>
size_t i2=i;<br>
while(v-- && i2<m){<br>
auto si=stride(a,i2);<br>
i2+=si;<br>
}<br>
// assert(v==-1);<br>
enforce(v==-1);<br>
return a[i..i2];<br>
}<br>
unittest{<br>
import std.range;<br>
auto a="≈açç√ef";<br>
auto b=a.slice(2,6);<br>
assert(a.slice(2,6)=="çç√e");<br>
assert(a.slice(2,6).ptr==a.<u></u>slice(2,3).ptr);<br>
assert(a.slice(0,a.walkLength) is a);<br>
import std.exception;<br>
assertThrown(a.slice(2,8));<br>
assertThrown(a.slice(2,1));<br>
}<br>
<br>
</blockquote>
<br></div></div>
Another one, with negative index like Javascript's String.slice():<br>
<a href="http://dpaste.dzfl.pl/608435c5" target="_blank">http://dpaste.dzfl.pl/608435c5</a><br>
</blockquote></div><br></div><div class="gmail_extra">not as efficient as what I proposed since it's iterating over the string twice (the 2nd index redoes the work done by 1st index). Could be adapted though.</div><div class="gmail_extra">
<br></div><div class="gmail_extra"><br></div></div>