strorder.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (C) 1995, 1996, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/chars.h"
  44. #include "libguile/validate.h"
  45. #include "libguile/strorder.h"
  46. SCM_DEFINE1 (scm_string_equal_p, "string=?", scm_tc7_rpsubr,
  47. (SCM s1, SCM s2),
  48. "Lexicographic equality predicate; \n"
  49. "Returns @t{#t} if the two strings are the same length and contain the same\n"
  50. "characters in the same positions, otherwise returns @t{#f}. (r5rs)\n\n"
  51. "@samp{String-ci=?} treats\n"
  52. "upper and lower case letters as though they were the same character, but\n"
  53. "@samp{string=?} treats upper and lower case as distinct characters.")
  54. #define FUNC_NAME s_scm_string_equal_p
  55. {
  56. register scm_sizet i;
  57. register unsigned char *c1, *c2;
  58. SCM_VALIDATE_ROSTRING (1,s1);
  59. SCM_VALIDATE_ROSTRING (2,s2);
  60. i = SCM_ROLENGTH (s2);
  61. if (SCM_ROLENGTH (s1) != i)
  62. {
  63. return SCM_BOOL_F;
  64. }
  65. c1 = SCM_ROUCHARS (s1);
  66. c2 = SCM_ROUCHARS (s2);
  67. while (0 != i--)
  68. if (*c1++ != *c2++)
  69. return SCM_BOOL_F;
  70. return SCM_BOOL_T;
  71. }
  72. #undef FUNC_NAME
  73. SCM_DEFINE1 (scm_string_ci_equal_p, "string-ci=?", scm_tc7_rpsubr,
  74. (SCM s1, SCM s2),
  75. "Case-insensitive string equality predicate; returns @t{#t} if\n"
  76. "the two strings are the same length and their component characters\n"
  77. "match (ignoring case) at each position; otherwise returns @t{#f}. (r5rs)")
  78. #define FUNC_NAME s_scm_string_ci_equal_p
  79. {
  80. register scm_sizet i;
  81. register unsigned char *c1, *c2;
  82. SCM_VALIDATE_ROSTRING (1,s1);
  83. SCM_VALIDATE_ROSTRING (2,s2);
  84. i = SCM_ROLENGTH (s2);
  85. if (SCM_ROLENGTH (s1) != i)
  86. {
  87. return SCM_BOOL_F;
  88. }
  89. c1 = SCM_ROUCHARS (s1);
  90. c2 = SCM_ROUCHARS (s2);
  91. while (0 != i--)
  92. if (scm_upcase(*c1++) != scm_upcase(*c2++))
  93. return SCM_BOOL_F;
  94. return SCM_BOOL_T;
  95. }
  96. #undef FUNC_NAME
  97. SCM_DEFINE1 (scm_string_less_p, "string<?", scm_tc7_rpsubr,
  98. (SCM s1, SCM s2),
  99. "Lexicographic ordering predicate; returns @t{#t} if @var{s1}\n"
  100. "is lexicographically less than @var{s2}. (r5rs)")
  101. #define FUNC_NAME s_scm_string_less_p
  102. {
  103. register scm_sizet i, len, s2len;
  104. register unsigned char *c1, *c2;
  105. register int c;
  106. SCM_VALIDATE_ROSTRING (1,s1);
  107. SCM_VALIDATE_ROSTRING (2,s2);
  108. len = SCM_ROLENGTH (s1);
  109. s2len = SCM_ROLENGTH (s2);
  110. if (len>s2len) len = s2len;
  111. c1 = SCM_ROUCHARS (s1);
  112. c2 = SCM_ROUCHARS (s2);
  113. for (i = 0;i<len;i++) {
  114. c = (*c1++ - *c2++);
  115. if (c>0)
  116. return SCM_BOOL_F;
  117. if (c<0)
  118. return SCM_BOOL_T;
  119. }
  120. {
  121. SCM answer;
  122. answer = SCM_BOOL(s2len != len);
  123. return answer;
  124. }
  125. }
  126. #undef FUNC_NAME
  127. SCM_DEFINE1 (scm_string_leq_p, "string<=?", scm_tc7_rpsubr,
  128. (SCM s1, SCM s2),
  129. "Lexicographic ordering predicate; returns @t{#t} if @var{s1}\n"
  130. "is lexicographically less than or equal to @var{s2}. (r5rs)")
  131. #define FUNC_NAME s_scm_string_leq_p
  132. {
  133. return SCM_BOOL_NOT (scm_string_less_p (s2, s1));
  134. }
  135. #undef FUNC_NAME
  136. SCM_DEFINE1 (scm_string_gr_p, "string>?", scm_tc7_rpsubr,
  137. (SCM s1, SCM s2),
  138. "Lexicographic ordering predicate; returns @t{#t} if @var{s1}\n"
  139. "is lexicographically greater than @var{s2}. (r5rs)")
  140. #define FUNC_NAME s_scm_string_gr_p
  141. {
  142. return scm_string_less_p (s2, s1);
  143. }
  144. #undef FUNC_NAME
  145. SCM_DEFINE1 (scm_string_geq_p, "string>=?", scm_tc7_rpsubr,
  146. (SCM s1, SCM s2),
  147. "Lexicographic ordering predicate; returns @t{#t} if @var{s1}\n"
  148. "is lexicographically greater than or equal to @var{s2}. (r5rs)")
  149. #define FUNC_NAME s_scm_string_geq_p
  150. {
  151. return SCM_BOOL_NOT (scm_string_less_p (s1, s2));
  152. }
  153. #undef FUNC_NAME
  154. SCM_DEFINE1 (scm_string_ci_less_p, "string-ci<?", scm_tc7_rpsubr,
  155. (SCM s1, SCM s2),
  156. "Case insensitive lexicographic ordering predicate; \n"
  157. "returns @t{#t} if @var{s1} is lexicographically less than\n"
  158. "@var{s2} regardless of case. (r5rs)")
  159. #define FUNC_NAME s_scm_string_ci_less_p
  160. {
  161. register scm_sizet i, len, s2len;
  162. register unsigned char *c1, *c2;
  163. register int c;
  164. SCM_VALIDATE_ROSTRING (1,s1);
  165. SCM_VALIDATE_ROSTRING (2,s2);
  166. len = SCM_ROLENGTH (s1);
  167. s2len = SCM_ROLENGTH (s2);
  168. if (len>s2len) len = s2len;
  169. c1 = SCM_ROUCHARS (s1);
  170. c2 = SCM_ROUCHARS (s2);
  171. for (i = 0;i<len;i++) {
  172. c = (scm_upcase(*c1++) - scm_upcase(*c2++));
  173. if (c>0) return SCM_BOOL_F;
  174. if (c<0) return SCM_BOOL_T;
  175. }
  176. return SCM_BOOL(s2len != len);
  177. }
  178. #undef FUNC_NAME
  179. SCM_DEFINE1 (scm_string_ci_leq_p, "string-ci<=?", scm_tc7_rpsubr,
  180. (SCM s1, SCM s2),
  181. "Case insensitive lexicographic ordering predicate; \n"
  182. "returns @t{#t} if @var{s1} is lexicographically less than\n"
  183. "or equal to @var{s2} regardless of case. (r5rs)")
  184. #define FUNC_NAME s_scm_string_ci_leq_p
  185. {
  186. return SCM_BOOL_NOT (scm_string_ci_less_p (s2, s1));
  187. }
  188. #undef FUNC_NAME
  189. SCM_DEFINE1 (scm_string_ci_gr_p, "string-ci>?", scm_tc7_rpsubr,
  190. (SCM s1, SCM s2),
  191. "Case insensitive lexicographic ordering predicate; \n"
  192. "returns @t{#t} if @var{s1} is lexicographically greater than\n"
  193. "@var{s2} regardless of case. (r5rs)")
  194. #define FUNC_NAME s_scm_string_ci_gr_p
  195. {
  196. return scm_string_ci_less_p (s2, s1);
  197. }
  198. #undef FUNC_NAME
  199. SCM_DEFINE1 (scm_string_ci_geq_p, "string-ci>=?", scm_tc7_rpsubr,
  200. (SCM s1, SCM s2),
  201. "Case insensitive lexicographic ordering predicate; \n"
  202. "returns @t{#t} if @var{s1} is lexicographically greater than\n"
  203. "or equal to @var{s2} regardless of case. (r5rs)")
  204. #define FUNC_NAME s_scm_string_ci_geq_p
  205. {
  206. return SCM_BOOL_NOT (scm_string_ci_less_p (s1, s2));
  207. }
  208. #undef FUNC_NAME
  209. void
  210. scm_init_strorder ()
  211. {
  212. #include "libguile/strorder.x"
  213. }
  214. /*
  215. Local Variables:
  216. c-file-style: "gnu"
  217. End:
  218. */