Publisher review:Rmtrail.C Remove trailing whitespace from a string
Note: This can arguably be made better by eliminating the strlen() call and its attendant overhead, or by recoding it operate in a single pass rather than the "go to the end, then back up" algorithm used here. An example is in SNIPPETS - see NOTRAIL.C.
Hoever, a couple of points should be mentioned: 1. A library strlen() is often coded in assembly and, even with the call overhead, may be faster than the same functionality written in C.2. Furthermore, the algorithm in NOTRAIL.C has to potentially examine every character in a string twice (once to compare to NUL, the other to test for whitespace). That it does this in one pass or two is immaterial. By using the two pass approach here, we can take potential advantage of strlen to check for NUL's, then explicitly only test backwards from the end looking for whitespace. For non-trivial strings (i.e. mostly text with a few spaces at the end), this may well be faster. 3. If the potential increase in performance is really *that* important, the most sensible improvement would be o rewite it in assembly.4. The additional algorithmic complexity of the improvements in NOTRAIL.C may arguably decrease maintainability. By contrast, the algorithm here is quite obvious.
Rmtrail.C is a C/C++ script for Snippets scripts design by Bob Stout.
It runs on following operating system: Windows / Linux / BSD / Solaris.
Operating system:Windows / Linux / BSD / Solaris