Mimic Python's strip() function in C

后端 未结 4 1240
旧时难觅i
旧时难觅i 2021-01-12 22:29

I started on a little toy project in C lately and have been scratching my head over the best way to mimic the strip() functionality that is part of the python string objects

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 23:00

    I wrote C code to implement this function. I also wrote a few trivial tests to make sure my function does sensible things.

    This function writes to a buffer you provide, and should never write past the end of the buffer, so it should not be prone to buffer overflow security issues.

    Note: only Test() uses stdio.h, so if you just need the function, you only need to include ctype.h (for isspace()) and string.h (for strlen()).

    // strstrip.c -- implement white space stripping for a string in C
    //
    // This code is released into the public domain.
    //
    // You may use it for any purpose whatsoever, and you don't need to advertise
    // where you got it, but you aren't allowed to sue me for giving you free
    // code; all the risk of using this is yours.
    
    
    
    #include 
    #include 
    #include 
    
    
    
    // strstrip() -- strip leading and trailing white space from a string
    //
    // Copies from sIn to sOut, writing at most lenOut characters.
    //
    // Returns number of characters in returned string, or -1 on an error.
    // If you get -1 back, then nothing was written to sOut at all.
    
    int
    strstrip(char *sOut, unsigned int lenOut, char const *sIn)
    {
        char const *pStart, *pEnd;
        unsigned int len;
        char *pOut;
    
        // if there is no room for any output, or a null pointer, return error!
        if (0 == lenOut || !sIn || !sOut)
            return -1;
    
        pStart = sIn;
        pEnd = sIn + strlen(sIn) - 1;
    
        // skip any leading whitespace
        while (*pStart && isspace(*pStart))
            ++pStart;
    
        // skip any trailing whitespace
        while (pEnd >= sIn && isspace(*pEnd))
            --pEnd;
    
        pOut = sOut;
        len = 0;
    
        // copy into output buffer
        while (pStart <= pEnd && len < lenOut - 1)
        {
            *pOut++ = *pStart++;
            ++len;
        }
    
    
        // ensure output buffer is properly terminated
        *pOut = '\0';
        return len;
    }
    
    
    void
    Test(const char *s)
    {
        int len;
        char buf[1024];
    
        len = strstrip(buf, sizeof(buf), s);
    
        if (!s)
            s = "**null**";  // don't ask printf to print a null string
        if (-1 == len)
            *buf = '\0';  // don't ask printf to print garbage from buf
    
        printf("Input: \"%s\"  Result: \"%s\" (%d chars)\n", s, buf, len);
    }
    
    
    main()
    {
        Test(NULL);
        Test("");
        Test(" ");
        Test("    ");
        Test("x");
        Test("  x");
        Test("  x   ");
        Test("  x y z   ");
        Test("x y z");
    }
    

提交回复
热议问题