YAZ  5.34.0
matchstr.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <yaz/yaz-iconv.h>
19 #include <yaz/matchstr.h>
20 
21 int yaz_strcasecmp(const char *s1, const char *s2)
22 {
23  return yaz_strncasecmp(s1, s2, strlen(s1) + 1);
24 }
25 
26 int yaz_strncasecmp(const char *s1, const char *s2, size_t n)
27 {
28  while (n--)
29  {
30  unsigned char c1 = *s1++;
31  unsigned char c2 = *s2++;
32  if (yaz_isupper(c1))
33  c1 = yaz_tolower(c1);
34  if (yaz_isupper(c2))
35  c2 = yaz_tolower(c2);
36  if (c1 != c2)
37  return c1 - c2;
38  }
39  return 0;
40 }
41 
42 int yaz_matchstr(const char *s1, const char *s2)
43 {
44  while (*s1 && *s2)
45  {
46  unsigned char c1 = *s1;
47  unsigned char c2 = *s2;
48 
49  if (c2 == '?')
50  return 0;
51  if (c1 == '-')
52  c1 = *++s1;
53  if (c2 == '-')
54  c2 = *++s2;
55  if (!c1 || !c2)
56  break;
57  if (c2 != '.')
58  {
59  if (yaz_isupper(c1))
60  c1 = yaz_tolower(c1);
61  if (yaz_isupper(c2))
62  c2 = yaz_tolower(c2);
63  if (c1 != c2)
64  break;
65  }
66  s1++;
67  s2++;
68  }
69  return *s1 || *s2;
70 }
71 
72 int yaz_strcmp_del(const char *a, const char *b, const char *b_del)
73 {
74  while (*a && *b)
75  {
76  if (*a != *b)
77  return *a - *b;
78  a++;
79  b++;
80  }
81  if (b_del && strchr(b_del, *b))
82  return *a;
83  return *a - *b;
84 }
85 
86 int yaz_memcmp(const void *a, const void *b, size_t len_a, size_t len_b)
87 {
88  size_t m_len = len_a < len_b ? len_a : len_b;
89  int r = memcmp(a, b, m_len);
90  if (r)
91  return r;
92  return len_a - len_b;
93 }
94 
95 int yaz_strcmp_null(const char *v1, const char *v2)
96 {
97  if (v1)
98  {
99  if (v2)
100  return strcmp(v1, v2);
101  else
102  return 1;
103  }
104  else if (v2)
105  return -1;
106  return 0;
107 }
108 
109 /*
110  * Local variables:
111  * c-basic-offset: 4
112  * c-file-style: "Stroustrup"
113  * indent-tabs-mode: nil
114  * End:
115  * vim: shiftwidth=4 tabstop=8 expandtab
116  */
117 
int yaz_memcmp(const void *a, const void *b, size_t len_a, size_t len_b)
compares two buffers of different size
Definition: matchstr.c:86
int yaz_strcmp_null(const char *v1, const char *v2)
strcmp, null may be passed
Definition: matchstr.c:95
int yaz_strcmp_del(const char *a, const char *b, const char *b_del)
match a and b with some delimitor for b
Definition: matchstr.c:72
int yaz_strcasecmp(const char *s1, const char *s2)
ala strcasecmp - no locale
Definition: matchstr.c:21
int yaz_matchstr(const char *s1, const char *s2)
match strings - independent of case and '-'
Definition: matchstr.c:42
int yaz_strncasecmp(const char *s1, const char *s2, size_t n)
ala strncasecmp - no locale
Definition: matchstr.c:26
Header for YAZ iconv interface.
Header for YAZ iconv interface.
#define yaz_tolower(x)
Definition: yaz-iconv.h:90
#define yaz_isupper(x)
Definition: yaz-iconv.h:89