string.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2016 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * nasmlib.c library routines for the Netwide Assembler
  35. */
  36. #include "compiler.h"
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39. #include "nasmlib.h"
  40. /*
  41. * Prepare a table of tolower() results. This avoids function calls
  42. * on some platforms.
  43. */
  44. unsigned char nasm_tolower_tab[256];
  45. void tolower_init(void)
  46. {
  47. int i;
  48. for (i = 0; i < 256; i++)
  49. nasm_tolower_tab[i] = tolower(i);
  50. }
  51. #ifndef nasm_stricmp
  52. int nasm_stricmp(const char *s1, const char *s2)
  53. {
  54. unsigned char c1, c2;
  55. int d;
  56. while (1) {
  57. c1 = nasm_tolower(*s1++);
  58. c2 = nasm_tolower(*s2++);
  59. d = c1-c2;
  60. if (d)
  61. return d;
  62. if (!c1)
  63. break;
  64. }
  65. return 0;
  66. }
  67. #endif
  68. #ifndef nasm_strnicmp
  69. int nasm_strnicmp(const char *s1, const char *s2, size_t n)
  70. {
  71. unsigned char c1, c2;
  72. int d;
  73. while (n--) {
  74. c1 = nasm_tolower(*s1++);
  75. c2 = nasm_tolower(*s2++);
  76. d = c1-c2;
  77. if (d)
  78. return d;
  79. if (!c1)
  80. break;
  81. }
  82. return 0;
  83. }
  84. #endif
  85. int nasm_memicmp(const char *s1, const char *s2, size_t n)
  86. {
  87. unsigned char c1, c2;
  88. int d;
  89. while (n--) {
  90. c1 = nasm_tolower(*s1++);
  91. c2 = nasm_tolower(*s2++);
  92. d = c1-c2;
  93. if (d)
  94. return d;
  95. }
  96. return 0;
  97. }
  98. #ifndef nasm_strsep
  99. char *nasm_strsep(char **stringp, const char *delim)
  100. {
  101. char *s = *stringp;
  102. char *e;
  103. if (!s)
  104. return NULL;
  105. e = strpbrk(s, delim);
  106. if (e)
  107. *e++ = '\0';
  108. *stringp = e;
  109. return s;
  110. }
  111. #endif
  112. /* skip leading spaces */
  113. char *nasm_skip_spaces(const char *p)
  114. {
  115. if (p)
  116. while (*p && nasm_isspace(*p))
  117. p++;
  118. return (char *)p;
  119. }
  120. /* skip leading non-spaces */
  121. char *nasm_skip_word(const char *p)
  122. {
  123. if (p)
  124. while (*p && !nasm_isspace(*p))
  125. p++;
  126. return (char *)p;
  127. }
  128. /* zap leading spaces with zero */
  129. char *nasm_zap_spaces_fwd(char *p)
  130. {
  131. if (p)
  132. while (*p && nasm_isspace(*p))
  133. *p++ = 0x0;
  134. return p;
  135. }
  136. /* zap spaces with zero in reverse order */
  137. char *nasm_zap_spaces_rev(char *p)
  138. {
  139. if (p)
  140. while (*p && nasm_isspace(*p))
  141. *p-- = 0x0;
  142. return p;
  143. }
  144. /* zap leading and trailing spaces */
  145. char *nasm_trim_spaces(char *p)
  146. {
  147. p = nasm_zap_spaces_fwd(p);
  148. nasm_zap_spaces_fwd(nasm_skip_word(p));
  149. return p;
  150. }
  151. /*
  152. * return the word extracted from a stream
  153. * or NULL if nothing left
  154. */
  155. char *nasm_get_word(char *p, char **tail)
  156. {
  157. char *word = nasm_skip_spaces(p);
  158. char *next = nasm_skip_word(word);
  159. if (word && *word) {
  160. if (*next)
  161. *next++ = '\0';
  162. } else
  163. word = next = NULL;
  164. /* NOTE: the tail may start with spaces */
  165. *tail = next;
  166. return word;
  167. }
  168. /*
  169. * Extract "opt=val" values from the stream and
  170. * returns "opt"
  171. *
  172. * Exceptions:
  173. * 1) If "=val" passed the NULL returned though
  174. * you may continue handling the tail via "next"
  175. * 2) If "=" passed the NULL is returned and "val"
  176. * is set to NULL as well
  177. */
  178. char *nasm_opt_val(char *p, char **val, char **next)
  179. {
  180. char *q, *nxt;
  181. *val = *next = NULL;
  182. p = nasm_get_word(p, &nxt);
  183. if (!p)
  184. return NULL;
  185. q = strchr(p, '=');
  186. if (q) {
  187. if (q == p)
  188. p = NULL;
  189. *q++='\0';
  190. if (*q) {
  191. *val = q;
  192. } else {
  193. q = nasm_get_word(q + 1, &nxt);
  194. if (q)
  195. *val = q;
  196. }
  197. } else {
  198. q = nasm_skip_spaces(nxt);
  199. if (q && *q == '=') {
  200. q = nasm_get_word(q + 1, &nxt);
  201. if (q)
  202. *val = q;
  203. }
  204. }
  205. *next = nxt;
  206. return p;
  207. }