Logo Search packages:      
Sourcecode: likewise-open version File versions

bool trim_string ( char *  s,
const char *  front,
const char *  back 
)

Trim the specified elements off the front and back of a string.

Definition at line 568 of file util_str.c.

{
      bool ret = false;
      size_t front_len;
      size_t back_len;
      size_t len;

      /* Ignore null or empty strings. */
      if (!s || (s[0] == '\0'))
            return false;

      front_len   = front? strlen(front) : 0;
      back_len    = back? strlen(back) : 0;

      len = strlen(s);

      if (front_len) {
            while (len && strncmp(s, front, front_len)==0) {
                  /* Must use memmove here as src & dest can
                   * easily overlap. Found by valgrind. JRA. */
                  memmove(s, s+front_len, (len-front_len)+1);
                  len -= front_len;
                  ret=true;
            }
      }

      if (back_len) {
            while ((len >= back_len) &&
                        strncmp(s+len-back_len,back,back_len)==0) {
                  s[len-back_len]='\0';
                  len -= back_len;
                  ret=true;
            }
      }
      return ret;
}


Generated by  Doxygen 1.6.0   Back to index