Added function count()

This commit is contained in:
Andrei Alexandrescu 2009-10-26 22:45:37 +00:00
parent c12b96b726
commit 449025e4d7

View file

@ -1514,40 +1514,6 @@ unittest
assert(lastSequence("1000\u20AC") == "\u20AC".length); assert(lastSequence("1000\u20AC") == "\u20AC".length);
} }
/**
Returns the total number of code points encoded in a string.
The input to this function MUST be validly encoded. This is enforced
by the function's in-contract.
Supercedes: This function supercedes $(D std.utf.toUCSindex()).
Standards: Unicode 5.0, ASCII, ISO-8859-1, WINDOWS-1252
Params:
s = the string to be counted
*/
uint count(E)(const(E)[] s)
in
{
assert(isValid(s));
}
body
{
uint n = 0;
while (s.length != 0)
{
EncoderInstance!(E).skip(s);
++n;
}
return n;
}
unittest
{
assert(count("\u20AC100") == 4);
}
/** /**
Returns the array index at which the (n+1)th code point begins. Returns the array index at which the (n+1)th code point begins.