Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
J

Jamercee

@Jamercee
About
Posts
1
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Familiarity : Trimming leading and trailing spaces from a string...
    J Jamercee

    Not sure if you are joking, or looking for a serious answer. The code you posted makes WAY too many passes through the source string. If you're looking for maximum performance, you can get this down to just a single pass thusly:

    LPSTR* nxt = wName;

    // find 1st non-whitespace char
    while(*nxt && isspace(*nxt))
    nxt++;

    // if nothing but whitespace
    if (!*np) {
    wName = '\0';
    return wName;
    }

    // save position of 1st non-whitespace
    LPSTR* start = nxt;

    // now find last non-whitespace character
    // before end of line

    LPSTR* last_no_ws = nxt;
    while(*nxt) {
    if (!isspace(*nxt))
    last_no_ws = nxt;
    nxt++;
    }

    // how long is our non-whitespace string?
    int len = (last_no_ws - start) + 1;

    // move the string to beginning of wName
    memmove(wName, start, len);
    wName[len] = '\0';

    return wName;

    You could even save the 'memove' at the end by just modifying the wName string in place (add null after last_no_ws), then return the reference to 'start'. :)

    The Weird and The Wonderful
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups