Prev: CROSS: looking for someone who can translate my software interface to German, French and italian language
Next: How to keep a sorted list of 2D points in a loop?
From: Lothar Behrens on 28 Mar 2010 11:12 Hi, I have implemented a trim function and documented it as follows: /** \brief Trim trailing spaces. * * Removes the trailing spaces in the string. */ void trim(); But actually I have written a test to ensure it's correctness and assumed it would remove leading and trailing whitespaces. I know there are rtrim and ltrim in some languages or implementations. So I think I am wrong with my implementation as I have expected other behavior than I have initially implemented. What would you expect from this function? Thanks Lothar
From: Moi on 28 Mar 2010 11:36 On Sun, 28 Mar 2010 08:12:23 -0700, Lothar Behrens wrote: > Hi, > > I have implemented a trim function and documented it as follows: > > /** \brief Trim trailing spaces. > * > * Removes the trailing spaces in the string. */ > void trim(); > > > What would you expect from this function? > I would expect it to accept an argument, presumably a char * pointing to the string at hand. HTH, AvK
From: Lothar Behrens on 28 Mar 2010 11:41 On 28 Mrz., 17:36, Moi <r...(a)invalid.address.org> wrote: > On Sun, 28 Mar 2010 08:12:23 -0700, Lothar Behrens wrote: > > Hi, > > > I have implemented a trim function and documented it as follows: > > > /** \brief Trim trailing spaces. > > * > > * Removes the trailing spaces in the string. */ > > void trim(); > > > What would you expect from this function? > > I would expect it to accept an argument, presumably a char * pointing to > the string at hand. > > HTH, > AvK Actually this is a method in my string class. But also .NET documented to trim leading and trailing whitespaces. In C, assuming I have the char* parameter, what would you expect the function will do? Lothar
From: Moi on 28 Mar 2010 11:56 On Sun, 28 Mar 2010 08:41:00 -0700, Lothar Behrens wrote: > > Actually this is a method in my string class. But also .NET documented > to trim leading and trailing whitespaces. > > In C, assuming I have the char* parameter, what would you expect the > function will do? I'd expect it to do what it says: trim whitespace. There are some naming issues, wrt where to trim, and what to trim. *Where* to trim : begin of string end of string, both... You could name them ltrim() and rtrim() and lrtrim or squeeze() *What* to trim: do you want to depend on the definition of isspace() (or similar) ? Is a backspace a space ? is a formfeed a space ? a bell ? a non-breakable space ? Do you want to depend on a particular character set ? HTH, AvK
From: Lothar Behrens on 28 Mar 2010 12:45
On 28 Mrz., 17:56, Moi <r...(a)invalid.address.org> wrote: > On Sun, 28 Mar 2010 08:41:00 -0700, Lothar Behrens wrote: > > > Actually this is a method in my string class. But also .NET documented > > to trim leading and trailing whitespaces. > > > In C, assuming I have the char* parameter, what would you expect the > > function will do? > > I'd expect it to do what it says: trim whitespace. > > There are some naming issues, wrt where to trim, and what to trim. > > *Where* to trim : begin of string end of string, both... > You could name them ltrim() and rtrim() and lrtrim or squeeze() > > *What* to trim: do you want to depend on the definition of isspace() (or similar) ? > Is a backspace a space ? is a formfeed a space ? a bell ? a non-breakable space ? > > Do you want to depend on a particular character set ? > > HTH, > AvK Ok, there are much more issues. Even I do not support unicode yet, with unicode it would properly much more complicated. My implementation is a very dumb and easy one: /** \brief Trim trailing spaces. * * Removes the trailing spaces in the string. */ void LB_STDCALL lbString::trim() { while (stringdata[strlen(stringdata)-1] == ' ') // stringdata is of type char* stringdata[strlen(stringdata)-1] = 0; } I'll let my test fail by assuming it will do ltrim + rtrim and correct the function after if I have more tests to ensure that changing this function will not break other code (TDD) :-) Also I haven't thought about the other cases (character set, backspace, nonprintable chars, ...) Lothar |