View Raw SPL
/****************************************************************************
*                                                                           *
*   STRTRIM.SPL    Copyright 2018 (C) DSP Development Corporation           *
*                                                                           *
*   Author:        Randy Race                                               *
*                                                                           *
*   Synopsis:      Skips leading whitespace in a string                     *
*                                                                           *
*   Revisions:     24 Jan 2018     RRR     Creation                         *
*                                                                           *
****************************************************************************/

#if @HELP_STRTRIM

    STRTRIM

    Purpose: Skips leading whitespace of a string.

    Syntax:  STRTRIM("string", mode, trimchars)

                string - A string, input string to process.

                  mode - Optional. An integer, the trim mode:

                          0: trim trailing whitespace
                          1: trim leading whitespace (default)
                          2: trim both leading and trailing whitespace

              trimchars - Optional. A string, the characters to trim.
                          Defaults to spaces, tabs and newlines.
                          
    Returns: A string where the leading and/or trailing whitespace
             has been eliminated.

    Example:
             strtrim("   aaa bbb")

             Returns the string "aaa bbb".

    Example:
             str1 = "   " + strescape("\t") + "text";
             str2 = strtrim(str1)

             str2 == "text"

    Example:
             strtrim("   aaa bbb    ", 0)

             Returns the string "    aaa bbb".


    Example:
             strtrim("   aaa bbb    ", 2)

             Returns the string "aaa bbb".

    Example:
             strtrim("xyzaaa bbbxyxy", 2, "xyz")

             Returns the string "aaa bbb".

    Remarks:
             By default, STRTRIM removes leading and/or trailing
             whitespace from a string, including spaces, strchar(32),
             tabs, strchar(9) and newlines strescape("\n").

    See Also:
             Strchar
             Strescape
             Strfind
             Strget
#endif


/* skip leading whitespace */
strtrim(s, mode, trimchars)
{
        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) s = "";

                        mode = 1;
                }

                if (not(isstr(s)))
                {
                        s = caststring(s);
                }

                /* tabs, newlines and spaces */
                trimchars = strescape("\t\n ");
        }

        switch (mode)
        {
                case 0:
                        /* trailing */
                        s = strrev(strtrim_trim(strrev(s), trimchars));
                        break;

                case 1:
                        /* leading */
                        s = strtrim_trim(s, trimchars);
                        break;

                case 2:
                default:
                        /* both */
                        s = strtrim_trim(s, trimchars);
                        s = strrev(strtrim_trim(strrev(s), trimchars));
                        break;
        }

        return(s);
}


/* skip leading whitespace */
strtrim_trim(s, trimchars)
{
        local p;

        p = strget(1, s, trimchars);

        if (strlen(p) > 0)
        {
                s = strfind(p, s);
        }
        else
        {
                /* all whitespace */
                s = "";
        }

        return(s);
}