Skip to content

Commit

Permalink
Merge pull request #657 from jmdavis/codeLength
Browse files Browse the repository at this point in the history
Added codeLength which works with a full string.
  • Loading branch information
9rnsr committed Jul 1, 2012
2 parents 93d176e + d683d13 commit 59c52b8
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions std/utf.d
Expand Up @@ -1129,6 +1129,7 @@ assert(codeLength!dchar('\U0010FFFF') == 1);
------
+/
ubyte codeLength(C)(dchar c) @safe pure nothrow
if(isSomeChar!C)
{
static if (C.sizeof == 1)
{
Expand Down Expand Up @@ -1163,6 +1164,90 @@ unittest
}


/++
Returns the number of code units that are required to encode $(D str)
in a string whose character type is $(D C). This is particularly useful
when slicing one string with the length of another and the two string
types use different character types.
Examples:
------
assert(codeLength!char("hello world") ==
to!string("hello world").length);
assert(codeLength!wchar("hello world") ==
to!wstring("hello world").length);
assert(codeLength!dchar("hello world") ==
to!dstring("hello world").length);
assert(codeLength!char(`プログラミング`) ==
to!string(`プログラミング`).length);
assert(codeLength!wchar(`プログラミング`) ==
to!wstring(`プログラミング`).length);
assert(codeLength!dchar(`プログラミング`) ==
to!dstring(`プログラミング`).length);
string haystack = `Être sans la verité, ça, ce ne serait pas bien.`;
wstring needle = `Être sans la verité`;
assert(haystack[codeLength!char(needle) .. $] ==
`, ça, ce ne serait pas bien.`);
------
+/
size_t codeLength(C1, C2)(C2[] str) @safe pure
if(isSomeChar!C1 && isSomeChar!C2)
{
static if(is(Unqual!C1 == Unqual!C2))
return str.length;
else
{
size_t total = 0;

foreach(dchar c; str)
total += codeLength!C1(c);

return total;
}
}

//Verify Examples.
unittest
{
assert(codeLength!char("hello world") ==
to!string("hello world").length);
assert(codeLength!wchar("hello world") ==
to!wstring("hello world").length);
assert(codeLength!dchar("hello world") ==
to!dstring("hello world").length);

assert(codeLength!char(`プログラミング`) ==
to!string(`プログラミング`).length);
assert(codeLength!wchar(`プログラミング`) ==
to!wstring(`プログラミング`).length);
assert(codeLength!dchar(`プログラミング`) ==
to!dstring(`プログラミング`).length);

string haystack = `Être sans la verité, ça, ce ne serait pas bien.`;
wstring needle = `Être sans la verité`;
assert(haystack[codeLength!char(needle) .. $] ==
`, ça, ce ne serait pas bien.`);
}

unittest
{
foreach(S; TypeTuple!(char[], const char[], string,
wchar[], const wchar[], wstring,
dchar[], const dchar[], dstring))
{
foreach(C; TypeTuple!(char, wchar, dchar))
{
assert(codeLength!C(to!S("Walter Bright")) == to!(C[])("Walter Bright").length);
assert(codeLength!C(to!S(`言語`)) == to!(C[])(`言語`).length);
assert(codeLength!C(to!S(`ウェブサイト@La_Verité.com`)) ==
to!(C[])(`ウェブサイト@La_Verité.com`).length);
}
}
}


/* =================== Validation ======================= */

/++
Expand Down

0 comments on commit 59c52b8

Please sign in to comment.