Created
March 18, 2023 19:16
-
-
Save crazymonkyyy/233b0c109f132693c97fac3cdf7c1162 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isascii(dchar c){ | |
return c <dchar(127); | |
} | |
auto processtext(dstring s){ | |
import std.sumtype; | |
struct range{ | |
dstring s; | |
bool isunicode; | |
long i,j; | |
alias sumtype=SumType!(string,dchar); | |
sumtype front(){ | |
if(isunicode){ | |
return sumtype(s[i..j].to!dchar); | |
} else { | |
return sumtype(s[i..j].to!string); | |
}} | |
void popFront(){ | |
i=j; | |
if(empty){return;} | |
if( ! isunicode){ | |
isunicode=true; | |
j=i+1; | |
//j=s[i..$].countUntil!(a=>!a.isascii); | |
} else {//if(isunicode) | |
if(s[j].isascii){ | |
j=s[i..$].countUntil!(a=>!a.isascii)+i; | |
isunicode=false; | |
} else { | |
j++; | |
}} | |
} | |
auto pop(){ | |
popFront; | |
return this; | |
} | |
bool empty(){ | |
return j>=s.length; | |
} | |
} | |
bool isascii=s.length!=0?s[0].isascii:false; | |
return range(s,!isascii).pop; | |
} | |
unittest{ | |
dstring foo="Lorem ipsum dolor sit amet, δ½ ε₯½, consectetur adipiscing elit. π Morbi euismod, γγγ«γ‘γ―, quam at tincidunt pulvinar, tortor metus bibendum mauris, eget vulputate lacus elit sit amet elit. Duis ullamcorper ligula at lectus bibendum, πΆπ± neque consectetur dapibus. Nullam eget augue ac metus maximus volutpat. π"; | |
import std.stdio; | |
foo.processtext.each!writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment