strorder.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (C) 1995, 1996, 1999, 2000, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/chars.h"
  23. #include "libguile/strings.h"
  24. #include "libguile/symbols.h"
  25. #include "libguile/validate.h"
  26. #include "libguile/strorder.h"
  27. #include "libguile/srfi-13.h"
  28. SCM_C_INLINE_KEYWORD static SCM
  29. srfi13_cmp (SCM s1, SCM s2, SCM (*cmp) (SCM, SCM, SCM, SCM, SCM, SCM))
  30. {
  31. if (scm_is_true (cmp (s1, s2,
  32. SCM_UNDEFINED, SCM_UNDEFINED,
  33. SCM_UNDEFINED, SCM_UNDEFINED)))
  34. return SCM_BOOL_T;
  35. else
  36. return SCM_BOOL_F;
  37. }
  38. static SCM scm_i_string_equal_p (SCM s1, SCM s2, SCM rest);
  39. SCM_DEFINE (scm_i_string_equal_p, "string=?", 0, 2, 1,
  40. (SCM s1, SCM s2, SCM rest),
  41. "Lexicographic equality predicate; return @code{#t} if the two\n"
  42. "strings are the same length and contain the same characters in\n"
  43. "the same positions, otherwise return @code{#f}.\n"
  44. "\n"
  45. "The procedure @code{string-ci=?} treats upper and lower case\n"
  46. "letters as though they were the same character, but\n"
  47. "@code{string=?} treats upper and lower case as distinct\n"
  48. "characters.")
  49. #define FUNC_NAME s_scm_i_string_equal_p
  50. {
  51. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  52. return SCM_BOOL_T;
  53. while (!scm_is_null (rest))
  54. {
  55. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_eq)))
  56. return SCM_BOOL_F;
  57. s1 = s2;
  58. s2 = scm_car (rest);
  59. rest = scm_cdr (rest);
  60. }
  61. return srfi13_cmp (s1, s2, scm_string_eq);
  62. }
  63. #undef FUNC_NAME
  64. SCM scm_string_equal_p (SCM s1, SCM s2)
  65. #define FUNC_NAME s_scm_i_string_equal_p
  66. {
  67. return srfi13_cmp (s1, s2, scm_string_eq);
  68. }
  69. #undef FUNC_NAME
  70. static SCM scm_i_string_ci_equal_p (SCM s1, SCM s2, SCM rest);
  71. SCM_DEFINE (scm_i_string_ci_equal_p, "string-ci=?", 0, 2, 1,
  72. (SCM s1, SCM s2, SCM rest),
  73. "Case-insensitive string equality predicate; return @code{#t} if\n"
  74. "the two strings are the same length and their component\n"
  75. "characters match (ignoring case) at each position; otherwise\n"
  76. "return @code{#f}.")
  77. #define FUNC_NAME s_scm_i_string_ci_equal_p
  78. {
  79. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  80. return SCM_BOOL_T;
  81. while (!scm_is_null (rest))
  82. {
  83. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ci_eq)))
  84. return SCM_BOOL_F;
  85. s1 = s2;
  86. s2 = scm_car (rest);
  87. rest = scm_cdr (rest);
  88. }
  89. return srfi13_cmp (s1, s2, scm_string_ci_eq);
  90. }
  91. #undef FUNC_NAME
  92. SCM scm_string_ci_equal_p (SCM s1, SCM s2)
  93. #define FUNC_NAME s_scm_i_string_ci_equal_p
  94. {
  95. return srfi13_cmp (s1, s2, scm_string_ci_eq);
  96. }
  97. #undef FUNC_NAME
  98. static SCM scm_i_string_less_p (SCM s1, SCM s2, SCM rest);
  99. SCM_DEFINE (scm_i_string_less_p, "string<?", 0, 2, 1,
  100. (SCM s1, SCM s2, SCM rest),
  101. "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
  102. "is lexicographically less than @var{s2}.")
  103. #define FUNC_NAME s_scm_i_string_less_p
  104. {
  105. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  106. return SCM_BOOL_T;
  107. while (!scm_is_null (rest))
  108. {
  109. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_lt)))
  110. return SCM_BOOL_F;
  111. s1 = s2;
  112. s2 = scm_car (rest);
  113. rest = scm_cdr (rest);
  114. }
  115. return srfi13_cmp (s1, s2, scm_string_lt);
  116. }
  117. #undef FUNC_NAME
  118. SCM scm_string_less_p (SCM s1, SCM s2)
  119. #define FUNC_NAME s_scm_i_string_less_p
  120. {
  121. return srfi13_cmp (s1, s2, scm_string_lt);
  122. }
  123. #undef FUNC_NAME
  124. static SCM scm_i_string_leq_p (SCM s1, SCM s2, SCM rest);
  125. SCM_DEFINE (scm_i_string_leq_p, "string<=?", 0, 2, 1,
  126. (SCM s1, SCM s2, SCM rest),
  127. "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
  128. "is lexicographically less than or equal to @var{s2}.")
  129. #define FUNC_NAME s_scm_i_string_leq_p
  130. {
  131. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  132. return SCM_BOOL_T;
  133. while (!scm_is_null (rest))
  134. {
  135. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_le)))
  136. return SCM_BOOL_F;
  137. s1 = s2;
  138. s2 = scm_car (rest);
  139. rest = scm_cdr (rest);
  140. }
  141. return srfi13_cmp (s1, s2, scm_string_le);
  142. }
  143. #undef FUNC_NAME
  144. SCM scm_string_leq_p (SCM s1, SCM s2)
  145. #define FUNC_NAME s_scm_i_string_leq_p
  146. {
  147. return srfi13_cmp (s1, s2, scm_string_le);
  148. }
  149. #undef FUNC_NAME
  150. static SCM scm_i_string_gr_p (SCM s1, SCM s2, SCM rest);
  151. SCM_DEFINE (scm_i_string_gr_p, "string>?", 0, 2, 1,
  152. (SCM s1, SCM s2, SCM rest),
  153. "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
  154. "is lexicographically greater than @var{s2}.")
  155. #define FUNC_NAME s_scm_i_string_gr_p
  156. {
  157. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  158. return SCM_BOOL_T;
  159. while (!scm_is_null (rest))
  160. {
  161. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_gt)))
  162. return SCM_BOOL_F;
  163. s1 = s2;
  164. s2 = scm_car (rest);
  165. rest = scm_cdr (rest);
  166. }
  167. return srfi13_cmp (s1, s2, scm_string_gt);
  168. }
  169. #undef FUNC_NAME
  170. SCM scm_string_gr_p (SCM s1, SCM s2)
  171. #define FUNC_NAME s_scm_i_string_gr_p
  172. {
  173. return srfi13_cmp (s1, s2, scm_string_gt);
  174. }
  175. #undef FUNC_NAME
  176. static SCM scm_i_string_geq_p (SCM s1, SCM s2, SCM rest);
  177. SCM_DEFINE (scm_i_string_geq_p, "string>=?", 0, 2, 1,
  178. (SCM s1, SCM s2, SCM rest),
  179. "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
  180. "is lexicographically greater than or equal to @var{s2}.")
  181. #define FUNC_NAME s_scm_i_string_geq_p
  182. {
  183. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  184. return SCM_BOOL_T;
  185. while (!scm_is_null (rest))
  186. {
  187. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ge)))
  188. return SCM_BOOL_F;
  189. s1 = s2;
  190. s2 = scm_car (rest);
  191. rest = scm_cdr (rest);
  192. }
  193. return srfi13_cmp (s1, s2, scm_string_ge);
  194. }
  195. #undef FUNC_NAME
  196. SCM scm_string_geq_p (SCM s1, SCM s2)
  197. #define FUNC_NAME s_scm_i_string_geq_p
  198. {
  199. return srfi13_cmp (s1, s2, scm_string_ge);
  200. }
  201. #undef FUNC_NAME
  202. static SCM scm_i_string_ci_less_p (SCM s1, SCM s2, SCM rest);
  203. SCM_DEFINE (scm_i_string_ci_less_p, "string-ci<?", 0, 2, 1,
  204. (SCM s1, SCM s2, SCM rest),
  205. "Case insensitive lexicographic ordering predicate; return\n"
  206. "@code{#t} if @var{s1} is lexicographically less than @var{s2}\n"
  207. "regardless of case.")
  208. #define FUNC_NAME s_scm_i_string_ci_less_p
  209. {
  210. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  211. return SCM_BOOL_T;
  212. while (!scm_is_null (rest))
  213. {
  214. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ci_lt)))
  215. return SCM_BOOL_F;
  216. s1 = s2;
  217. s2 = scm_car (rest);
  218. rest = scm_cdr (rest);
  219. }
  220. return srfi13_cmp (s1, s2, scm_string_ci_lt);
  221. }
  222. #undef FUNC_NAME
  223. SCM scm_string_ci_less_p (SCM s1, SCM s2)
  224. #define FUNC_NAME s_scm_i_string_ci_less_p
  225. {
  226. return srfi13_cmp (s1, s2, scm_string_ci_lt);
  227. }
  228. #undef FUNC_NAME
  229. static SCM scm_i_string_ci_leq_p (SCM s1, SCM s2, SCM rest);
  230. SCM_DEFINE (scm_i_string_ci_leq_p, "string-ci<=?", 0, 2, 1,
  231. (SCM s1, SCM s2, SCM rest),
  232. "Case insensitive lexicographic ordering predicate; return\n"
  233. "@code{#t} if @var{s1} is lexicographically less than or equal\n"
  234. "to @var{s2} regardless of case.")
  235. #define FUNC_NAME s_scm_i_string_ci_leq_p
  236. {
  237. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  238. return SCM_BOOL_T;
  239. while (!scm_is_null (rest))
  240. {
  241. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ci_le)))
  242. return SCM_BOOL_F;
  243. s1 = s2;
  244. s2 = scm_car (rest);
  245. rest = scm_cdr (rest);
  246. }
  247. return srfi13_cmp (s1, s2, scm_string_ci_le);
  248. }
  249. #undef FUNC_NAME
  250. SCM scm_string_ci_leq_p (SCM s1, SCM s2)
  251. #define FUNC_NAME s_scm_i_string_ci_leq_p
  252. {
  253. return srfi13_cmp (s1, s2, scm_string_ci_le);
  254. }
  255. #undef FUNC_NAME
  256. static SCM scm_i_string_ci_gr_p (SCM s1, SCM s2, SCM rest);
  257. SCM_DEFINE (scm_i_string_ci_gr_p, "string-ci>?", 0, 2, 1,
  258. (SCM s1, SCM s2, SCM rest),
  259. "Case insensitive lexicographic ordering predicate; return\n"
  260. "@code{#t} if @var{s1} is lexicographically greater than\n"
  261. "@var{s2} regardless of case.")
  262. #define FUNC_NAME s_scm_i_string_ci_gr_p
  263. {
  264. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  265. return SCM_BOOL_T;
  266. while (!scm_is_null (rest))
  267. {
  268. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ci_gt)))
  269. return SCM_BOOL_F;
  270. s1 = s2;
  271. s2 = scm_car (rest);
  272. rest = scm_cdr (rest);
  273. }
  274. return srfi13_cmp (s1, s2, scm_string_ci_gt);
  275. }
  276. #undef FUNC_NAME
  277. SCM scm_string_ci_gr_p (SCM s1, SCM s2)
  278. #define FUNC_NAME s_scm_i_string_ci_gr_p
  279. {
  280. return srfi13_cmp (s1, s2, scm_string_ci_gt);
  281. }
  282. #undef FUNC_NAME
  283. static SCM scm_i_string_ci_geq_p (SCM s1, SCM s2, SCM rest);
  284. SCM_DEFINE (scm_i_string_ci_geq_p, "string-ci>=?", 0, 2, 1,
  285. (SCM s1, SCM s2, SCM rest),
  286. "Case insensitive lexicographic ordering predicate; return\n"
  287. "@code{#t} if @var{s1} is lexicographically greater than or\n"
  288. "equal to @var{s2} regardless of case.")
  289. #define FUNC_NAME s_scm_i_string_ci_geq_p
  290. {
  291. if (SCM_UNBNDP (s1) || SCM_UNBNDP (s2))
  292. return SCM_BOOL_T;
  293. while (!scm_is_null (rest))
  294. {
  295. if (scm_is_false (srfi13_cmp (s1, s2, scm_string_ci_ge)))
  296. return SCM_BOOL_F;
  297. s1 = s2;
  298. s2 = scm_car (rest);
  299. rest = scm_cdr (rest);
  300. }
  301. return srfi13_cmp (s1, s2, scm_string_ci_ge);
  302. }
  303. #undef FUNC_NAME
  304. SCM scm_string_ci_geq_p (SCM s1, SCM s2)
  305. #define FUNC_NAME s_scm_i_string_ci_geq_p
  306. {
  307. return srfi13_cmp (s1, s2, scm_string_ci_ge);
  308. }
  309. #undef FUNC_NAME
  310. void
  311. scm_init_strorder ()
  312. {
  313. #include "libguile/strorder.x"
  314. }
  315. /*
  316. Local Variables:
  317. c-file-style: "gnu"
  318. End:
  319. */