Andreas Krennmair:
[apps/madmutt.git] / ascii.c
1 /*
2  * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
3  * 
4  *     This program is free software; you can redistribute it
5  *     and/or modify it under the terms of the GNU General Public
6  *     License as published by the Free Software Foundation; either
7  *     version 2 of the License, or (at your option) any later
8  *     version.
9  * 
10  *     This program is distributed in the hope that it will be
11  *     useful, but WITHOUT ANY WARRANTY; without even the implied
12  *     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  *     PURPOSE.  See the GNU General Public License for more
14  *     details.
15  * 
16  *     You should have received a copy of the GNU General Public
17  *     License along with this program; if not, write to the Free
18  *     Software Foundation, Inc., 59 Temple Place - Suite 330,
19  *     Boston, MA 02111, USA.
20  * 
21  */
22
23
24 /* 
25  * Versions of the string comparison functions which are
26  * locale-insensitive.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "ascii.h"
32
33 int ascii_isupper (int c)
34 {
35   return (c >= 'A') && (c <= 'Z');
36 }
37
38 int ascii_islower (int c)
39 {
40   return (c >= 'a') && (c <= 'z');
41 }
42
43 int ascii_toupper (int c)
44 {
45   if (ascii_islower (c))
46     return c & ~32;
47   
48   return c;
49 }
50
51 int ascii_tolower (int c)
52 {
53   if (ascii_isupper (c))
54     return c | 32;
55   
56   return c;
57 }
58
59 int ascii_strcasecmp (const char *a, const char *b)
60 {
61   int i;
62   
63   if (a == b)
64     return 0;
65   if (a == NULL && b)
66     return -1;
67   if (b == NULL && a)
68     return 1;
69   
70   for (; *a || *b; a++, b++)
71   {
72     if ((i = ascii_tolower (*a) - ascii_tolower (*b)))
73       return i;
74   }
75   
76   return 0;
77 }
78
79 int ascii_strncasecmp (const char *a, const char *b, int n)
80 {
81   int i, j;
82   
83   if (a == b)
84     return 0;
85   if (a == NULL && b)
86     return -1;
87   if (b == NULL && a)
88     return 1;
89   
90   for (j = 0; (*a || *b) && j < n; a++, b++, j++)
91   {
92     if ((i = ascii_tolower (*a) - ascii_tolower (*b)))
93       return i;
94   }
95   
96   return 0;
97 }