http://d.puremagic.com/issues/show_bug.cgi?id=6232

           Summary: And idea for std.string.toStringz docs
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: websites
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2011-06-30 15:50:44 PDT ---
To the online docs of std.string.toStringz I suggest to add an example like
this, that shows how to use the D type system to avoid passing not
zero-terminated char* to C functions (C functions coming from C libraries,
etc):


import std.string: toStringz;

struct CcharPtr {
    const char* ptr;
    alias ptr this;
}

// example of C function
extern(C) size_t strlen(CcharPtr str);

CcharPtr toStringz2(string s) {
    return CcharPtr(toStringz(s));
}

void main() {
    string s1 = "this is ";
    string s2 = s1 ~ "just a string";
    auto cs = toStringz2(s2);
    assert(s2.length == strlen(cs));
}

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

Reply via email to