www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12448] New: "in" argument for std.string.toStringz

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12448

           Summary: "in" argument for std.string.toStringz
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2014-03-23 20:15:27 PDT ---
The signature of the second overload of toStringz is:

immutable(char)* toStringz(string s)  trusted pure nothrow 

If you replace it with:

immutable(char)* toStringz(in string s)  trusted pure nothrow

Then the compiler gets able to catch some not used result bugs:


import std.exception: assumeUnique;
import core.stdc.string: memcmp, strlen;
import std.array: empty;

immutable(char)* toStringz(const(char)[] s)  trusted pure nothrow {
    auto copy = new char[s.length + 1];
    copy[0 .. s.length] = s[];
    copy[$ - 1] = '\0';
    return assumeUnique(copy).ptr;
}

immutable(char)* toStringz(in string s)  trusted pure nothrow {
    if (s.empty)
        return "".ptr;
    immutable p = s.ptr + s.length;
    if ((cast(size_t) p & 3) && *p == 0)
        return s.ptr;
    return toStringz(cast(const char[]) s);
}

void main() {
    string foo;
    foo.toStringz; // Warning
}



test.d(32,8): Warning: Call to strictly pure function test.toStringz discards
return value of type immutable(char)*, prepend a cast(void) if intentional

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2014
parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12448



--- Comment #1 from bearophile_hugs eml.cc 2014-03-23 20:17:01 PDT ---
To avoid future possible problems with "scope", using "const string s" is also
enough.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2014