Hide most of the cases of slicing based on strlen using the toDString() helper

This commit is contained in:
Stian Gulpen 2019-11-18 04:35:03 +01:00
parent d73ce548f9
commit 3e8dced0e7
25 changed files with 75 additions and 64 deletions

View file

@ -81,9 +81,11 @@ overrides unless the overriding function does something different (as far as
the caller is concerned) than the overridden function. Ddoc comment blocks
are often overkill for nested functions and function literals; use ordinary
comments for those. Follow the [D Style](https://dlang.org/dstyle.html#phobos_documentation)
for for comment blocks.
for comment blocks.
3. Do not use `strlen`/`strcmp` and their ilk. Use D arrays instead.
If slicing from a `char*` is required then use `dmd.utils.toDString`
or the member function `.toString` that is implemented in many classes.
4. Use `ref`/`out` instead of raw pointers.
@ -158,7 +160,7 @@ Such functions should not be mutating the data nor issuing error messages.
across the source tree is likely to be disruptive and unlikely to provide
significant improvement.
3. Reformatting into your personal style. Please stick with the existing style.
3. Reformatting into your personal style. Please stick with the existing style.
Use the [D Style](https://dlang.org/dstyle.html#phobos_documentation) for new code.
As always, treating the above as sacred writ is a huge mistake. Use

View file

@ -742,7 +742,7 @@ extern (C++) final class VersionCondition : DVCondition
deprecated("Kept for C++ compat - Use the string overload instead")
static void addPredefinedGlobalIdent(const(char)* ident)
{
addPredefinedGlobalIdent(ident[0 .. ident.strlen]);
addPredefinedGlobalIdent(ident.toDString());
}
/// Ditto

View file

@ -35,6 +35,7 @@ import dmd.root.aav;
import dmd.target;
import dmd.tokens;
import dmd.utf;
import dmd.utils;
import dmd.visitor;
private immutable char[TMAX] mangleChar =
@ -577,7 +578,7 @@ public:
case LINK.cpp:
{
const p = target.cpp.toMangle(d);
return p[0 .. strlen(p)];
return p.toDString();
}
case LINK.default_:
case LINK.system:

View file

@ -414,7 +414,7 @@ extern(C++) void gendocfile(Module m)
time(&t);
char* p = ctime(&t);
p = mem.xstrdup(p);
m.macrotable.define("DATETIME", p[0 .. strlen(p)]);
m.macrotable.define("DATETIME", p.toDString());
m.macrotable.define("YEAR", p[20 .. 20 + 4]);
}
const srcfilename = m.srcfile.toString();

View file

@ -314,13 +314,17 @@ extern (C++) class Dsymbol : ASTNode
return ident is null;
}
extern(D) private const(char)[] prettyFormatHelper()
{
const cstr = toPrettyChars();
return '`' ~ cstr.toDString() ~ "`\0";
}
final void error(const ref Loc loc, const(char)* format, ...)
{
va_list ap;
va_start(ap, format);
const cstr = toPrettyChars();
const pretty = '`' ~ cstr[0 .. strlen(cstr)] ~ "`\0";
.verror(loc, format, ap, kind(), pretty.ptr);
.verror(loc, format, ap, kind(), prettyFormatHelper().ptr);
va_end(ap);
}
@ -328,10 +332,8 @@ extern (C++) class Dsymbol : ASTNode
{
va_list ap;
va_start(ap, format);
const cstr = toPrettyChars();
const pretty = '`' ~ cstr[0 .. strlen(cstr)] ~ "`\0";
const loc = getLoc();
.verror(loc, format, ap, kind(), pretty.ptr);
.verror(loc, format, ap, kind(), prettyFormatHelper().ptr);
va_end(ap);
}
@ -339,9 +341,7 @@ extern (C++) class Dsymbol : ASTNode
{
va_list ap;
va_start(ap, format);
const cstr = toPrettyChars();
const pretty = '`' ~ cstr[0 .. strlen(cstr)] ~ "`\0";
.vdeprecation(loc, format, ap, kind(), pretty.ptr);
.vdeprecation(loc, format, ap, kind(), prettyFormatHelper().ptr);
va_end(ap);
}
@ -349,10 +349,8 @@ extern (C++) class Dsymbol : ASTNode
{
va_list ap;
va_start(ap, format);
const cstr = toPrettyChars();
const pretty = '`' ~ cstr[0 .. strlen(cstr)] ~ "`\0";
const loc = getLoc();
.vdeprecation(loc, format, ap, kind(), pretty.ptr);
.vdeprecation(loc, format, ap, kind(), prettyFormatHelper().ptr);
va_end(ap);
}

View file

@ -20,6 +20,7 @@ import dmd.globals;
import dmd.root.outbuffer;
import dmd.root.rmem;
import dmd.console;
import dmd.utils;
nothrow:
@ -474,7 +475,7 @@ private void verrorPrint(const ref Loc loc, Color headerColor, const(char)* head
!global.params.mixinOut)
{
import dmd.filecache : FileCache;
auto fllines = FileCache.fileCache.addOrGetFile(loc.filename[0 .. strlen(loc.filename)]);
auto fllines = FileCache.fileCache.addOrGetFile(loc.filename.toDString());
if (loc.linnum - 1 < fllines.lines.length)
{

View file

@ -65,6 +65,7 @@ import dmd.target;
import dmd.tokens;
import dmd.typesem;
import dmd.utf;
import dmd.utils;
import dmd.visitor;
enum LOGSEMANTIC = false;
@ -2304,7 +2305,7 @@ extern (C++) final class StringExp : Expression
static StringExp create(Loc loc, char* s)
{
return new StringExp(loc, s[0 .. strlen(s)]);
return new StringExp(loc, s.toDString());
}
static StringExp create(Loc loc, void* string, size_t len)
@ -2315,7 +2316,7 @@ extern (C++) final class StringExp : Expression
// Same as create, but doesn't allocate memory.
static void emplace(UnionExp* pue, Loc loc, char* s)
{
emplaceExp!(StringExp)(pue, loc, s[0 .. strlen(s)]);
emplaceExp!(StringExp)(pue, loc, s.toDString());
}
extern (D) static void emplace(UnionExp* pue, Loc loc, const(void)[] string)
@ -6575,7 +6576,7 @@ extern (C++) final class FileInitExp : DefaultInitExp
else
s = loc.isValid() ? loc.filename : sc._module.ident.toChars();
Expression e = new StringExp(loc, s[0 .. strlen(s)]);
Expression e = new StringExp(loc, s.toDString());
e = e.expressionSemantic(sc);
e = e.castTo(sc, type);
return e;
@ -6620,8 +6621,8 @@ extern (C++) final class ModuleInitExp : DefaultInitExp
override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
const char* s = (sc.callsc ? sc.callsc : sc)._module.toPrettyChars();
Expression e = new StringExp(loc, s[0 .. strlen(s)]);
const auto s = (sc.callsc ? sc.callsc : sc)._module.toPrettyChars().toDString();
Expression e = new StringExp(loc, s);
e = e.expressionSemantic(sc);
e = e.castTo(sc, type);
return e;
@ -6651,7 +6652,7 @@ extern (C++) final class FuncInitExp : DefaultInitExp
s = sc.func.Dsymbol.toPrettyChars();
else
s = "";
Expression e = new StringExp(loc, s[0 .. strlen(s)]);
Expression e = new StringExp(loc, s.toDString());
e = e.expressionSemantic(sc);
e.type = Type.tstring;
return e;
@ -6691,7 +6692,7 @@ extern (C++) final class PrettyFuncInitExp : DefaultInitExp
s = "";
}
Expression e = new StringExp(loc, s[0 .. strlen(s)]);
Expression e = new StringExp(loc, s.toDString());
e = e.expressionSemantic(sc);
e.type = Type.tstring;
return e;

View file

@ -13,7 +13,6 @@
module dmd.expressionsem;
import core.stdc.stdio;
import core.stdc.string;
import dmd.access;
import dmd.aggregate;
@ -8855,9 +8854,9 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
if (global.params.tracegc)
{
auto funcname = (sc.callsc && sc.callsc.func) ? sc.callsc.func.toPrettyChars() : sc.func.toPrettyChars();
arguments.push(new StringExp(exp.loc, exp.loc.filename[0 .. strlen(exp.loc.filename)]));
arguments.push(new StringExp(exp.loc, exp.loc.filename.toDString()));
arguments.push(new IntegerExp(exp.loc, exp.loc.linnum, Type.tint32));
arguments.push(new StringExp(exp.loc, funcname[0 .. strlen(funcname)]));
arguments.push(new StringExp(exp.loc, funcname.toDString()));
}
arguments.push(ale.e1);
arguments.push(exp.e2);

View file

@ -46,6 +46,7 @@ import dmd.statement_rewrite_walker;
import dmd.statement;
import dmd.statementsem;
import dmd.tokens;
import dmd.utils;
import dmd.visitor;
/// Inline Status
@ -2744,7 +2745,7 @@ private const(char)* prependSpace(const(char)* str)
{
if (!str || !*str) return "";
return (" " ~ str[0 .. strlen(str)] ~ "\0").ptr;
return (" " ~ str.toDString() ~ "\0").ptr;
}
/// Flag used by $(LREF resolveFuncCall).

View file

@ -63,7 +63,7 @@ public:
extern (D) this(const(char)* name) nothrow
{
//printf("Identifier('%s', %d)\n", name, value);
this(name[0 .. strlen(name)], TOK.identifier);
this(name.toDString(), TOK.identifier);
}
/// Sentinel for an anonymous identifier.

View file

@ -1050,7 +1050,7 @@ Returns: JsonFieldFlags.none on error, otherwise the JsonFieldFlags value
*/
extern (C++) JsonFieldFlags tryParseJsonField(const(char)* fieldName)
{
auto fieldNameString = fieldName[0 .. strlen(fieldName)];
auto fieldNameString = fieldName.toDString();
foreach (idx, enumName; __traits(allMembers, JsonFieldFlags))
{
static if (idx > 0)

View file

@ -224,7 +224,7 @@ final class LibElf : Library
return corrupt(__LINE__);
for (uint i = 0; i < nsymbols; i++)
{
const(char)[] name = s[0 .. strlen(s)];
const(char)[] name = s.toDString();
s += name.length + 1;
if (s - symtab > symtab_size)
return corrupt(__LINE__);
@ -254,7 +254,7 @@ final class LibElf : Library
om.length = cast(uint)buflen;
om.offset = 0;
auto n = cast(char*)FileName.name(module_name); // remove path, but not extension
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
om.name_offset = -1;
om.scan = 1;
if (fromfile)

View file

@ -153,7 +153,7 @@ final class LibMach : Library
om.length = cast(uint)(size + MachLibHeader.sizeof);
om.offset = 0;
const n = cast(const(char)*)(om.base + MachLibHeader.sizeof);
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
om.file_time = cast(uint)strtoul(header.file_time.ptr, &endptr, 10);
om.user_id = cast(uint)strtoul(header.user_id.ptr, &endptr, 10);
om.group_id = cast(uint)strtoul(header.group_id.ptr, &endptr, 10);
@ -209,7 +209,7 @@ final class LibMach : Library
om.length = cast(uint)buflen;
om.offset = 0;
const n = cast(const(char)*)FileName.name(module_name); // remove path, but not extension
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
om.scan = 1;
if (fromfile)
{

View file

@ -280,7 +280,7 @@ final class LibMSCoff : Library
char* s = string_table;
for (uint i = 0; i < number_of_symbols; i++)
{
const(char)[] name = s[0 .. strlen(s)];
const(char)[] name = s.toDString();
s += name.length + 1;
uint memi = indices[i] - 1;
if (memi >= number_of_members)
@ -310,7 +310,7 @@ final class LibMSCoff : Library
om.length = cast(uint)buflen;
om.offset = 0;
const(char)* n = global.params.preservePaths ? module_name : FileName.name(module_name); // remove path, but not extension
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
om.scan = 1;
if (fromfile)
{

View file

@ -149,7 +149,7 @@ final class LibOMF : Library
{
// Remove path and extension
auto n = cast(char*)Mem.check(strdup(FileName.name(module_name)));
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
char* ext = cast(char*)FileName.ext(n);
if (ext)
ext[-1] = 0;
@ -160,7 +160,7 @@ final class LibOMF : Library
* removing path and extension.
*/
auto n = cast(char*)Mem.check(strdup(FileName.name(name)));
om.name = n[0 .. strlen(n)];
om.name = n.toDString();
char* ext = cast(char*)FileName.ext(n);
if (ext)
ext[-1] = 0;

View file

@ -615,14 +615,14 @@ public int runLINK()
*/
static bool startsWith(const(char)* p, string needle)
{
const f = p[0 .. strlen(p)];
const f = p.toDString();
return f.length >= needle.length && f[0 .. needle.length] == needle;
}
// return true if flagp should be ordered in with the library flags
static bool flagIsLibraryRelated(const char* p)
{
const flag = p[0 .. strlen(p)];
const flag = p.toDString();
return startsWith(p, "-l") || startsWith(p, "-L")
|| flag == "-(" || flag == "-)"

View file

@ -356,10 +356,10 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
// Add in command line versions
if (params.versionids)
foreach (charz; *params.versionids)
VersionCondition.addGlobalIdent(charz[0 .. strlen(charz)]);
VersionCondition.addGlobalIdent(charz.toDString());
if (params.debugids)
foreach (charz; *params.debugids)
DebugCondition.addGlobalIdent(charz[0 .. strlen(charz)]);
DebugCondition.addGlobalIdent(charz.toDString());
setTarget(params);
@ -1304,7 +1304,7 @@ extern(C) void printGlobalConfigs(FILE* stream)
foreach (flag; dflags[])
{
bool needsQuoting;
foreach (c; flag[0 .. strlen(flag)])
foreach (c; flag.toDString())
{
if (!(isalnum(c) || c == '_'))
{
@ -1543,7 +1543,7 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
return buf;
}
const ident = ps + 1;
switch (ident[0 .. strlen(ident)])
switch (ident.toDString())
{
mixin(generateTransitionsText());
default:
@ -1563,7 +1563,7 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
for (size_t i = 1; i < arguments.dim; i++)
{
const(char)* p = arguments[i];
const(char)[] arg = p[0 .. strlen(p)];
const(char)[] arg = p.toDString();
if (*p != '-')
{
static if (TARGET.Windows)
@ -1876,7 +1876,7 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
if (Identifier.isValidIdentifier(p + len))
{
const ident = p + len;
switch (ident[0 .. strlen(ident)])
switch (ident.toDString())
{
case "baseline":
params.cpu = CPU.baseline;
@ -1970,7 +1970,7 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
else if (Identifier.isValidIdentifier(p + len))
{
const ident = p + len;
switch (ident[0 .. strlen(ident)])
switch (ident.toDString())
{
case "import":
params.bug10378 = true;

View file

@ -7694,7 +7694,7 @@ final class Parser(AST) : Lexer
case TOK.file:
{
const(char)* s = loc.filename ? loc.filename : mod.ident.toChars();
e = new AST.StringExp(loc, s[0 .. strlen(s)]);
e = new AST.StringExp(loc, s.toDString());
nextToken();
break;
}
@ -7702,7 +7702,7 @@ final class Parser(AST) : Lexer
{
assert(loc.isValid(), "__FILE_FULL_PATH__ does not work with an invalid location");
const s = FileName.toAbsolute(loc.filename);
e = new AST.StringExp(loc, s[0 .. strlen(s)]);
e = new AST.StringExp(loc, s.toDString());
nextToken();
break;
}
@ -7715,7 +7715,7 @@ final class Parser(AST) : Lexer
case TOK.moduleString:
{
const(char)* s = md ? md.toChars() : mod.toChars();
e = new AST.StringExp(loc, s[0 .. strlen(s)]);
e = new AST.StringExp(loc, s.toDString());
nextToken();
break;
}

View file

@ -17,6 +17,7 @@ import core.stdc.stdio;
import core.stdc.string;
import dmd.root.rmem;
import dmd.root.rootobject;
import dmd.utils;
debug
{
@ -124,7 +125,7 @@ struct OutBuffer
extern (C++) void writestring(const(char)* string) pure nothrow
{
write(string[0 .. strlen(string)]);
write(string.toDString);
}
void writestring(const(char)[] s) pure nothrow

View file

@ -16,6 +16,7 @@ module dmd.root.response;
import dmd.root.file;
import dmd.root.filename;
import dmd.utils;
///
alias responseExpand = responseExpandFrom!lookupInEnvironment;
@ -90,7 +91,7 @@ version (unittest)
char[] testEnvironment(const(char)* str) nothrow pure
{
import core.stdc.string: strlen;
switch (str[0 .. strlen(str)])
switch (str.toDString())
{
case "Foo":
return "foo @Bar #\0".dup;

View file

@ -18,7 +18,7 @@ import core.stdc.string, core.stdc.stdlib, core.sys.windows.winnt;
import dmd.root.rmem;
import dmd.globals, dmd.errors;
import dmd.globals, dmd.errors, dmd.utils;
private enum LOG = false;
@ -175,7 +175,7 @@ void scanMSCoffObjModule(void delegate(const(char)[] name, int pickAny) pAddSymb
default:
continue;
}
pAddSymbol(p[0 .. strlen(p)], 1);
pAddSymbol(p.toDString(), 1);
}
}

View file

@ -21,6 +21,7 @@ import dmd.root.rmem;
import dmd.root.outbuffer;
import dmd.arraytypes;
import dmd.errors;
import dmd.utils;
private enum LOG = false;
@ -115,7 +116,7 @@ void scanOmfObjModule(void delegate(const(char)[] name, int pickAny) pAddSymbol,
}
//printf("[s] name='%s'\n",name);
const(char)* n = names[idx];
pAddSymbol(n[0 .. strlen(n)], pickAny);
pAddSymbol(n.toDString(), pickAny);
break;
}
case COMDEF:

View file

@ -13,7 +13,6 @@
module dmd.statementsem;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.aliasthis;
@ -55,6 +54,7 @@ import dmd.statement;
import dmd.target;
import dmd.tokens;
import dmd.typesem;
import dmd.utils;
import dmd.visitor;
/*****************************************
@ -2654,7 +2654,7 @@ else
sl = new DotIdExp(ss.loc, sl, Id.__switch_error);
Expressions* args = new Expressions(2);
(*args)[0] = new StringExp(ss.loc, ss.loc.filename[0 .. strlen(ss.loc.filename)]);
(*args)[0] = new StringExp(ss.loc, ss.loc.filename.toDString());
(*args)[1] = new IntegerExp(ss.loc.linnum);
sl = new CallExp(ss.loc, sl, args);

View file

@ -13,7 +13,6 @@
module dmd.traits;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
@ -49,6 +48,7 @@ import dmd.typesem;
import dmd.visitor;
import dmd.root.rootobject;
import dmd.root.outbuffer;
import dmd.utils;
enum LOGSEMANTIC = false;
@ -1406,7 +1406,7 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
}
}
auto linkage = linkageToChars(link);
auto se = new StringExp(e.loc, linkage[0 .. strlen(linkage)]);
auto se = new StringExp(e.loc, linkage.toDString());
return se.expressionSemantic(sc);
}
if (e.ident == Id.allMembers ||
@ -1484,7 +1484,11 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
return 0;
// Avoid using strcmp in the first place due to the performance impact in an O(N^2) loop.
debug assert(strcmp(id.toChars(), sm.ident.toChars()) != 0);
debug
{
import core.stdc.string : strcmp;
assert(strcmp(id.toChars(), sm.ident.toChars()) != 0);
}
}
idents.push(sm.ident);
}
@ -1911,7 +1915,7 @@ Lnext:
}
auto exps = new Expressions(3);
(*exps)[0] = new StringExp(e.loc, s.loc.filename[0 .. strlen(s.loc.filename)]);
(*exps)[0] = new StringExp(e.loc, s.loc.filename.toDString());
(*exps)[1] = new IntegerExp(e.loc, s.loc.linnum,Type.tint32);
(*exps)[2] = new IntegerExp(e.loc, s.loc.charnum,Type.tint32);
auto tup = new TupleExp(e.loc, exps);

View file

@ -61,6 +61,7 @@ import dmd.sideeffect;
import dmd.target;
import dmd.tokens;
import dmd.typesem;
import dmd.utils;
/**************************
* This evaluates exp while setting length to be the number
@ -2123,7 +2124,7 @@ Expression getProperty(Type t, const ref Loc loc, Identifier ident, int flag)
}
else
{
e = new StringExp(loc, mt.deco[0 .. strlen(mt.deco)]);
e = new StringExp(loc, mt.deco.toDString());
Scope sc;
e = e.expressionSemantic(&sc);
}
@ -2131,7 +2132,7 @@ Expression getProperty(Type t, const ref Loc loc, Identifier ident, int flag)
else if (ident == Id.stringof)
{
const s = mt.toChars();
e = new StringExp(loc, s[0 .. strlen(s)]);
e = new StringExp(loc, s.toDString());
Scope sc;
e = e.expressionSemantic(&sc);
}