[Issue 1393] Phobos needs strnlen()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 14 18:10:07 PDT 2007


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


cdunn2001 at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME




------- Comment #1 from cdunn2001 at gmail.com  2007-08-14 20:10 -------
Derek Parnell suggested slicing as a solution:


import std.string;

/** Return a slice of the leftmost portion of 'x'
    up to but not including the first 'c' */
string lefts(string x, char c)
{
    int p;
    p = std.string.find(x,c);
    if (p < 0)
        p = x.length;
    return x[0..p];
}

int compare(string lhs, string rhs, char d = '\0')
{
    return std.string.cmp( lefts(lhs,d), lefts(rhs,d) );
}

and use it like ...

   char[32] NameA = '\0';
   char[56] NameB = '\0';

   NameA[0..5] = "derek";
   NameB[0..7] = "parnell";
   result = compare(NameA, NameB);


This is simple enough for me.


-- 



More information about the Digitalmars-d-bugs mailing list