1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2006 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * strfuncs.c -- String functions
49 #include "sphinxbase/ckd_alloc.h"
50 #include "sphinxbase/strfuncs.h"
52 /* Defined in dtoa.c */
53 double sb_strtod(const char *s00, char **se);
56 atof_c(char const *str)
58 return sb_strtod(str, NULL);
62 string_join(const char *base, ...)
71 while ((c = va_arg(args, const char *)) != NULL) {
77 out = ckd_calloc(len, 1);
80 while ((c = va_arg(args, const char *)) != NULL) {
89 string_trim(char *string, enum string_edge_e which)
94 if (which == STRING_START || which == STRING_BOTH) {
95 size_t sub = strspn(string, " \t\n\r\f");
97 memmove(string, string + sub, len + 1 - sub);
101 if (which == STRING_END || which == STRING_BOTH) {
104 if (strchr(" \t\n\r\f", string[sub]) == NULL)
109 string[sub+1] = '\0';
115 str2words(char *line, char **ptr, int32 max_ptr)
119 n = 0; /* #words found so far */
120 i = 0; /* For scanning through the input string */
122 /* Skip whitespace before next word */
123 while (line[i] && isspace((unsigned char)line[i]))
128 if (ptr != NULL && n >= max_ptr) {
130 * Pointer array size insufficient. Restore NULL chars inserted so far
131 * to space chars. Not a perfect restoration, but better than nothing.
140 /* Scan to end of word */
144 while (line[i] && !isspace((unsigned char)line[i]))
158 nextword(char *line, const char *delim, char **word, char *delimfound)
163 /* Skip past any preceding delimiters */
164 for (w = line; *w; w++) {
165 for (d = delim; *d && (*d != *w); d++);
172 *word = w; /* Beginning of word */
174 /* Skip until first delimiter char */
176 for (d = delim; *d && (*d != *w); d++);
181 /* Replace delimiter with NULL char, but return the original first */