head 1.7; access; symbols str_init:1.1.1.1 str:1.1.1; locks; strict; comment @ * @; 1.7 date 99.12.27.13.43.06; author rse; state dead; branches; next 1.6; 1.6 date 99.12.26.14.45.12; author rse; state Exp; branches; next 1.5; 1.5 date 99.12.25.20.57.23; author rse; state Exp; branches; next 1.4; 1.4 date 99.12.25.18.33.25; author rse; state Exp; branches; next 1.3; 1.3 date 99.12.25.18.23.02; author rse; state Exp; branches; next 1.2; 1.2 date 99.11.23.08.57.35; author rse; state Exp; branches; next 1.1; 1.1 date 99.11.22.17.29.12; author rse; state Exp; branches 1.1.1.1; next ; 1.1.1.1 date 99.11.22.17.29.12; author rse; state Exp; branches; next ; desc @@ 1.7 log @. @ text @/* ** Str - String Library ** Copyright (c) 1999 Ralf S. Engelschall ** ** This file is part of Str, a string handling and manipulation ** library which can be found at http://www.engelschall.com/sw/str/. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** str_handle.c: handling and manipulation functions */ #include "str_p.h" /* determine length of string */ size_t str_len(const char *s) { register char *t; if (s == NULL) return 0; t = (char *)s; while (*t++ != NUL) /*nop*/; return (t-s-1); } /* copy a string */ char *str_copy(char *as, const char *at, size_t n) { register char *s; register const char *t; char *rv; if (as == NULL || at == NULL) return NULL; if (n == 0) n = str_len(at); t = at; s = as; rv = as; if (s > t) { /* must go high to low */ t += n - 1; s += n; rv = s; *s-- = NUL; while (n-- > 0) *s-- = *t--; } else if (s < t) { /* must go low to high */ while (n-- > 0) *s++ = *t++; *s = NUL; rv = s; } return rv; } /* duplicate string */ char *str_dup(const char *s, str_size_t n) { char *t; char *rv; if (s == NULL) return NULL; if (n == 0) n = str_len(s); if ((rv = str_mem_alloc(n+1)) == NULL) return NULL; t = rv; while (n-- > 0) *t++ = *s++; *t = NUL; return rv; } /* concatenate one or more strings */ char *str_concat(char *s, ...) { va_list ap; va_list ap_safe; int n; char *rv; char *cp; char *ds; va_start(ap, s); if (s == NULL) return NULL; /* determine required target string length */ ap_safe = ap; n = str_len(s); while ((cp = va_arg(ap, char *)) != NULL) n += str_len(cp); ap = ap_safe; /* allocate target string */ if ((rv = str_mem_alloc(n+1)) == NULL) return NULL; /* concatenate the strings */ ds = rv; while ((*ds++ = *s++) != NUL) /*nop*/; while ((cp = va_arg(ap, char *)) != NULL) while ((*ds++ = *cp++) != NUL) /*nop*/; *ds = NUL; /* return target string */ va_end(ap); return rv; } /* splice a string into another */ char *str_splice(char *s, str_size_t off, str_size_t n, char *t, str_size_t m) { int sl; /* check for invalid arguments */ if (s == NULL || t == NULL) return NULL; /* check for overlapping areas */ if (!((t+m) <= s || (s+off+n) <= t)) return NULL; sl = str_len(s); if ((t+m) < s || (s+sl) < t) { /* splice _external_ area into internal area */ if (m != n) str_mem_move(s+off+m, s+off+n, sl-off-n+1); str_mem_move(s+off, t, m); } else { /* splice _internal_ area into internal area */ if (t > s) { /* move t from larger to lower address of s */ str_mem_rev(s+off, (t+m)-(s+off)); str_mem_rev(s+off, m); str_mem_rev(s+off+m, t-(s+n)); str_mem_move(t+m-n, t+m, (s+sl)-(t+m)+1); } else { /* move t from lower to higher address of s */ str_mem_rev(t, (s+off)-t); str_mem_rev(t, (s+off)-t-m); str_mem_rev(s+off-m, m); str_mem_move(s+off, s+off+n, sl-off-n+1); } } return s; } @ 1.6 log @*** empty log message *** @ text @@ 1.5 log @*** empty log message *** @ text @d87 1 a87 1 if ((rv = malloc(n+1)) == NULL) d118 1 a118 1 if ((rv = malloc(n+1)) == NULL) d138 3 d143 29 a171 1 return NULL; @ 1.4 log @*** empty log message *** @ text @d32 1 a32 1 size_t str_len(register const char *s) d34 1 a34 1 register int n; d37 5 a41 5 return NULL; n = 0; while (*s++ != NUL) n++; return n; d45 1 a45 1 char *str_copy(char *dst, const char *src, size_t bytes) d47 3 a49 2 register char *dst_p; register const char *src_p; d51 1 a51 1 if (src == NULL || dst == NULL) d53 6 a58 5 if (bytes == 0) bytes = str_len(src); src_p = src; dst_p = dst; if (dst > src) { d60 6 a65 7 src_p += bytes - 1; dst_p += bytes; *dst_p-- = NUL; while (bytes > 0) { *dst_p-- = *src_p--; bytes--; } d67 1 a67 1 else if (dst < src) { d69 4 a72 5 while (bytes > 0) { *dst_p++ = *src_p++; bytes--; } *dst_p = NUL; d74 1 a74 1 return dst_p; d80 2 a81 1 char *ds; d87 1 a87 1 if ((ds = malloc(n+1)) == NULL) d89 5 a93 2 str_copy(ds, s, n); return ds; a100 1 int n1; d102 1 a102 1 char *rc; d107 4 d112 1 a112 2 n1 = str_len(s); n = n1; d115 16 a130 12 if (n == n1) rc = str_dup(s, 0); else { if ((rc = malloc(n+1)) == NULL) return NULL; ds = rc; str_copy(cp, s, 0); ap = ap_safe; while ((cp = va_arg(ap, char *)) != NULL) ds = str_copy(ds, cp, 0); *ds = NUL; } d132 1 a132 1 return rc; d136 1 a136 1 char *str_splice(char *s, size_t so, size_t sl, char *i, size_t il) d138 2 @ 1.3 log @*** empty log message *** @ text @d29 1 a29 4 #include "str.h" #include #include @ 1.2 log @*** empty log message *** @ text @d1 3 a3 2 /* ** str_handle.c -- String Handling Functions d5 2 a6 2 ** ==================================================================== ** Copyright (c) 1999 Ralf S. Engelschall. All rights reserved. d8 4 a11 3 ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions ** are met: d13 12 a24 2 ** 1. Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. d26 1 a26 18 ** 2. Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** ** THIS SOFTWARE IS PROVIDED BY RALF S. ENGELSCHALL ``AS IS'' AND ANY ** EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RALF S. ENGELSCHALL OR ** ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, ** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED ** OF THE POSSIBILITY OF SUCH DAMAGE. ** ==================================================================== @ 1.1 log @Initial revision @ text @d60 2 d66 1 a66 1 src_p += bytes; d68 3 a70 1 while (bytes > 0 && *src_p != NUL) { a71 3 src_p--; dst_p--; *dst_p = *src_p; d76 2 a77 4 while (bytes > 0 && *src_p != NUL) { *dst_p = *src_p; dst_p++; src_p++; d80 1 a81 1 *dst_p = NUL; d86 1 a86 1 char *str_dup(const char *s) a88 1 int n; d92 2 a93 1 n = str_len(s); d96 1 a96 1 str_copy(ds, s, n+1); d118 1 a118 1 rc = str_dup(s); @ 1.1.1.1 log @ @ text @@