Skip to content

Commit

Permalink
refactor: replace StringExp.length with numberOfCodeUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Oct 23, 2015
1 parent 8c74caf commit 5674b95
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/dcast.d
Expand Up @@ -548,12 +548,11 @@ extern (C++) MATCH implicitConvTo(Expression e, Type t)
}
return;
}
int szto = cast(int)t.nextOf().size();
if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
{
if (e.committed && tynto != tyn)
return;
size_t fromlen = e.length(szto);
size_t fromlen = e.numberOfCodeUnits(tynto);
size_t tolen = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
if (tolen < fromlen)
return;
Expand All @@ -573,12 +572,11 @@ extern (C++) MATCH implicitConvTo(Expression e, Type t)
else if (e.type.ty == Tarray)
{
TY tynto = t.nextOf().ty;
int sznto = cast(int)t.nextOf().size();
if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
{
if (e.committed && tynto != tyn)
return;
size_t fromlen = e.length(sznto);
size_t fromlen = e.numberOfCodeUnits(tynto);
size_t tolen = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
if (tolen < fromlen)
return;
Expand Down
2 changes: 1 addition & 1 deletion src/dstruct.d
Expand Up @@ -742,7 +742,7 @@ public:
if (!se.committed &&
(typeb.ty == Tarray || typeb.ty == Tsarray) &&
(tynto == Tchar || tynto == Twchar || tynto == Tdchar) &&
se.length(cast(int)tb.nextOf().size()) < (cast(TypeSArray)tb).dim.toInteger())
se.numberOfCodeUnits(tynto) < (cast(TypeSArray)tb).dim.toInteger())
{
e = se.castTo(sc, t);
goto L1;
Expand Down
21 changes: 16 additions & 5 deletions src/expression.d
Expand Up @@ -4263,13 +4263,24 @@ public:
}

/**********************************
* Return the code unit count of string.
* Input:
* encSize code unit size of the target encoding.
* Return the number of code units the string would be if it were re-encoded
* as tynto.
* Params:
* tynto = code unit type of the target encoding
* Returns:
* number of code units
*/
size_t length(int encSize = 4)
final size_t numberOfCodeUnits(int tynto)
{
assert(encSize == 1 || encSize == 2 || encSize == 4);
int encSize;
switch (tynto)
{
case Tchar: encSize = 1; break;
case Twchar: encSize = 2; break;
case Tdchar: encSize = 4; break;
default:
assert(0);
}
if (sz == encSize)
return len;
size_t result = 0;
Expand Down
1 change: 0 additions & 1 deletion src/expression.h
Expand Up @@ -370,7 +370,6 @@ class StringExp : public Expression
static StringExp *create(Loc loc, char *s);
bool equals(RootObject *o);
Expression *semantic(Scope *sc);
size_t length(int encSize = 4);
StringExp *toStringExp();
StringExp *toUTF8(Scope *sc);
int compare(RootObject *obj);
Expand Down
5 changes: 4 additions & 1 deletion src/init.d
Expand Up @@ -880,7 +880,10 @@ public:
StringExp se = cast(StringExp)exp;
Type typeb = se.type.toBasetype();
TY tynto = tb.nextOf().ty;
if (!se.committed && (typeb.ty == Tarray || typeb.ty == Tsarray) && (tynto == Tchar || tynto == Twchar || tynto == Tdchar) && se.length(cast(int)tb.nextOf().size()) < (cast(TypeSArray)tb).dim.toInteger())
if (!se.committed &&
(typeb.ty == Tarray || typeb.ty == Tsarray) &&
(tynto == Tchar || tynto == Twchar || tynto == Tdchar) &&
se.numberOfCodeUnits(tynto) < (cast(TypeSArray)tb).dim.toInteger())
{
exp = se.castTo(sc, t);
goto L1;
Expand Down
2 changes: 1 addition & 1 deletion src/traits.d
Expand Up @@ -801,7 +801,7 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
}
ex = ex.ctfeInterpret();
StringExp se = ex.toStringExp();
if (!se || se.length() == 0)
if (!se || se.len == 0)
{
e.error("string expected as second argument of __traits %s instead of %s", e.ident.toChars(), ex.toChars());
return new ErrorExp();
Expand Down

0 comments on commit 5674b95

Please sign in to comment.