Hi,
This is an InputRange and RandomAccessRange combined; it's also the placement of wchar in possible null parts of dchar.
Please criticize this code. Each element of a UTF string is matched to its counterparts as dchar and used as wchar. The code is self-explanatory, do you think it's useful?
import std.stdio, std.algorithm;
import std.range, std.conv;
enum alphabets
{
u = "ÂABCÇDEFGHIJKLMNOPQRSTUVĞİWXYZÖŞÜÇÎÛ",
l = "âabcçdefghıjklmnopqrstuvğiwxyzöşüçîû",
ASCII65_95 = "AABCCDEFGHIJKLMNOPQRSTUVGIWXYZOSUCIU",
ASCII96_127 = "aabccdefghijklmnopqrstuvgiwxyzosuciu",
ASCII65_127 = ASCII65_95 ~ ASCII96_127
}
//enum dictU = alphabets.u.to!(wchar[]);
enum dictU = "ÂABCÇDEFGHIJKLMNOPQRSTUVĞİWXYZÖŞÜÇÎÛ".to!(wchar[]);
enum dictL = alphabets.l.to!(wchar[]);
struct MixString(T, T[] leftLiterals)
{
size_t index;
dchar[] dict;
this(string d)
{
// load dictionary
foreach(dchar c; d) dict ~= c;
// place counterparts
foreach(i, wchar c; leftLiterals)
{
dict[i] |= c << 16;
}
}
// input range functions
bool empty() { return index == dict.length; }
T front() { return dict[index] & 0x0000_FFFF; }
void popFront() { ++index; }
// search elements
auto nextIndexOf(wchar key)
{
scope(exit) index = 0;
size_t i = 1;
while(!empty)
{
if(front == key)
{
return i;
} else i++;
popFront();
}
return 0;
}
}
//
alias ConvUpper = MixString!(wchar, dictU);
alias ConvLower = MixString!(wchar, dictL);
void main()
{
auto test = ConvUpper(alphabets.l);/*
foreach(wchar c; test)
{
c.writefln!"%4X: %s"(c);
}//*/
string text = "fıstıkçı şâhap bir insandır!";
foreach(wchar c; text)
{
if(auto result = test.nextIndexOf(c))
{
wchar lookup = test.dict[result - 1] >> 16;
lookup.write;
} else
c.write;
}
writeln;
}
/*
FISTIKÇI ŞÂHAP BİR İNSANDIR!
*/
SDB@79