"Fossies" - the Fresh Open Source Software Archive

Member "tidy-html5-5.8.0/src/tmbstr.c" (16 Jul 2021, 4997 Bytes) of package /linux/www/tidy-html5-5.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tmbstr.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.4.0_vs_5.6.0.

    1 /* tmbstr.c -- Tidy string utility functions
    2 
    3   (c) 1998-2006 (W3C) MIT, ERCIM, Keio University
    4   See tidy.h for the copyright notice.
    5 
    6 */
    7 
    8 #include "forward.h"
    9 #include "tmbstr.h"
   10 #include "lexer.h"
   11 
   12 /* like strdup but using an allocator */
   13 tmbstr TY_(tmbstrdup)( TidyAllocator *allocator, ctmbstr str )
   14 {
   15     tmbstr s = NULL;
   16     if ( str )
   17     {
   18         uint len = TY_(tmbstrlen)( str );
   19         tmbstr cp = s = (tmbstr) TidyAlloc( allocator, 1+len );
   20         while ( 0 != (*cp++ = *str++) )
   21             /**/;
   22     }
   23     return s;
   24 }
   25 
   26 /* like strndup but using an allocator */
   27 tmbstr TY_(tmbstrndup)( TidyAllocator *allocator, ctmbstr str, uint len )
   28 {
   29     tmbstr s = NULL;
   30     if ( str && len > 0 )
   31     {
   32         tmbstr cp = s = (tmbstr) TidyAlloc( allocator, 1+len );
   33         while ( len-- > 0 &&  (*cp++ = *str++) )
   34           /**/;
   35         *cp = 0;
   36     }
   37     return s;
   38 }
   39 
   40 /* exactly same as strncpy */
   41 uint TY_(tmbstrncpy)( tmbstr s1, ctmbstr s2, uint size )
   42 {
   43     if ( s1 != NULL && s2 != NULL )
   44     {
   45         tmbstr cp = s1;
   46         while ( *s2 && --size )  /* Predecrement: reserve byte */
   47             *cp++ = *s2++;       /* for NULL terminator. */
   48         *cp = 0;
   49     }
   50     return size;
   51 }
   52 
   53 /* Allows expressions like:  cp += tmbstrcpy( cp, "joebob" );
   54 */
   55 uint TY_(tmbstrcpy)( tmbstr s1, ctmbstr s2 )
   56 {
   57     uint ncpy = 0;
   58     while (0 != (*s1++ = *s2++) )
   59         ++ncpy;
   60     return ncpy;
   61 }
   62 
   63 /* Allows expressions like:  cp += tmbstrcat( cp, "joebob" );
   64 */
   65 uint TY_(tmbstrcat)( tmbstr s1, ctmbstr s2 )
   66 {
   67     uint ncpy = 0;
   68     while ( *s1 )
   69         ++s1;
   70 
   71     while (0 != (*s1++ = *s2++) )
   72         ++ncpy;
   73     return ncpy;
   74 }
   75 
   76 /* exactly same as strcmp */
   77 int TY_(tmbstrcmp)( ctmbstr s1, ctmbstr s2 )
   78 {
   79     int c;
   80     while ((c = *s1) == *s2)
   81     {
   82         if (c == '\0')
   83             return 0;
   84 
   85         ++s1;
   86         ++s2;
   87     }
   88 
   89     return (*s1 > *s2 ? 1 : -1);
   90 }
   91 
   92 /* returns byte count, not char count */
   93 uint TY_(tmbstrlen)( ctmbstr str )
   94 {
   95     uint len = 0;
   96     if ( str ) 
   97     {
   98         while ( *str++ )
   99             ++len;
  100     }
  101     return len;
  102 }
  103 
  104 /*
  105  MS C 4.2 (and ANSI C) doesn't include strcasecmp.
  106  Note that tolower and toupper won't
  107  work on chars > 127.
  108 
  109  Neither does ToLower()!
  110 */
  111 int TY_(tmbstrcasecmp)( ctmbstr s1, ctmbstr s2 )
  112 {
  113     uint c;
  114 
  115     while (c = (uint)(*s1), TY_(ToLower)(c) == TY_(ToLower)((uint)(*s2)))
  116     {
  117         if (c == '\0')
  118             return 0;
  119 
  120         ++s1;
  121         ++s2;
  122     }
  123 
  124     return (*s1 > *s2 ? 1 : -1);
  125 }
  126 
  127 int TY_(tmbstrncmp)( ctmbstr s1, ctmbstr s2, uint n )
  128 {
  129     uint c;
  130 
  131     if (s1 == NULL || s2 == NULL)
  132     {
  133         if (s1 == s2)
  134             return 0;
  135         return (s1 == NULL ? -1 : 1);
  136     }
  137 
  138     while ((c = (byte)*s1) == (byte)*s2)
  139     {
  140         if (c == '\0')
  141             return 0;
  142 
  143         if (n == 0)
  144             return 0;
  145 
  146         ++s1;
  147         ++s2;
  148         --n;
  149     }
  150 
  151     if (n == 0)
  152         return 0;
  153 
  154     return (*s1 > *s2 ? 1 : -1);
  155 }
  156 
  157 int TY_(tmbstrncasecmp)( ctmbstr s1, ctmbstr s2, uint n )
  158 {
  159     uint c;
  160 
  161     while (c = (uint)(*s1), TY_(ToLower)(c) == TY_(ToLower)((uint)(*s2)))
  162     {
  163         if (c == '\0')
  164             return 0;
  165 
  166         if (n == 0)
  167             return 0;
  168 
  169         ++s1;
  170         ++s2;
  171         --n;
  172     }
  173 
  174     if (n == 0)
  175         return 0;
  176 
  177     return (*s1 > *s2 ? 1 : -1);
  178 }
  179 
  180 ctmbstr TY_(tmbsubstrn)( ctmbstr s1, uint len1, ctmbstr s2 )
  181 {
  182     uint len2 = TY_(tmbstrlen)(s2);
  183     int ix, diff = len1 - len2;
  184 
  185     for ( ix = 0; ix <= diff; ++ix )
  186     {
  187         if ( TY_(tmbstrncmp)(s1+ix, s2, len2) == 0 )
  188             return (ctmbstr) s1+ix;
  189     }
  190     return NULL;
  191 }
  192 
  193 ctmbstr TY_(tmbsubstr)( ctmbstr s1, ctmbstr s2 )
  194 {
  195     uint len1 = TY_(tmbstrlen)(s1), len2 = TY_(tmbstrlen)(s2);
  196     int ix, diff = len1 - len2;
  197 
  198     for ( ix = 0; ix <= diff; ++ix )
  199     {
  200         if ( TY_(tmbstrncasecmp)(s1+ix, s2, len2) == 0 )
  201             return (ctmbstr) s1+ix;
  202     }
  203     return NULL;
  204 }
  205 
  206 /* Transform ASCII chars in string to lower case */
  207 tmbstr TY_(tmbstrtolower)( tmbstr s )
  208 {
  209     tmbstr cp;
  210     for ( cp=s; *cp; ++cp )
  211         *cp = (tmbchar) TY_(ToLower)( *cp );
  212     return s;
  213 }
  214 
  215 /* Transform ASCII chars in string to upper case */
  216 tmbstr TY_(tmbstrtoupper)(tmbstr s)
  217 {
  218     tmbstr cp;
  219 
  220     for (cp = s; *cp; ++cp)
  221         *cp = (tmbchar)TY_(ToUpper)(*cp);
  222 
  223     return s;
  224 }
  225 
  226 int TY_(tmbvsnprintf)(tmbstr buffer, size_t count, ctmbstr format, va_list args)
  227 {
  228     int retval;
  229 
  230 #if HAS_VSNPRINTF
  231     retval = vsnprintf(buffer, count - 1, format, args);
  232     /* todo: conditionally null-terminate the string? */
  233     buffer[count - 1] = 0;
  234 #else
  235     retval = vsprintf(buffer, format, args);
  236 #endif /* HAS_VSNPRINTF */
  237     return retval;
  238 }
  239 
  240 int TY_(tmbsnprintf)(tmbstr buffer, size_t count, ctmbstr format, ...)
  241 {
  242     int retval;
  243     va_list args;
  244     va_start(args, format);
  245     retval = TY_(tmbvsnprintf)(buffer, count, format, args);
  246     va_end(args);
  247     return retval;
  248 }
  249 
  250 /*
  251  * local variables:
  252  * mode: c
  253  * indent-tabs-mode: nil
  254  * c-basic-offset: 4
  255  * eval: (c-set-offset 'substatement-open 0)
  256  * end:
  257  */