File indexing completed on 2024-04-14 14:19:53

0001 /*  -*- c++ -*-
0002     Copyright (c) 2005 Ingo Kloecker <kloecker@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kascii.h"
0021 
0022 int kasciistricmp(const char *str1, const char *str2)
0023 {
0024     const unsigned char *s1 = (const unsigned char *)str1;
0025     const unsigned char *s2 = (const unsigned char *)str2;
0026     int res;
0027     unsigned char c1, c2;
0028 
0029     if (!s1 || !s2) {
0030         return s1 ? 1 : (s2 ? -1 : 0);
0031     }
0032     if (!*s1 || !*s2) {
0033         return *s1 ? 1 : (*s2 ? -1 : 0);
0034     }
0035     for (; *s1; ++s1, ++s2) {
0036         c1 = *s1; c2 = *s2;
0037         if (c1 >= 'A' && c1 <= 'Z') {
0038             c1 += 'a' - 'A';
0039         }
0040         if (c2 >= 'A' && c2 <= 'Z') {
0041             c2 += 'a' - 'A';
0042         }
0043 
0044         if ((res = c1 - c2)) {
0045             break;
0046         }
0047     }
0048     return *s1 ? res : (*s2 ? -1 : 0);
0049 }
0050 
0051 /** Convert a single ASCII character to lowercase.
0052     @param ch Character to convert
0053     @internal
0054 */
0055 static unsigned char ASCIIToLower(unsigned char ch)
0056 {
0057     if (ch >= 'A' && ch <= 'Z') {
0058         return ch - 'A' + 'a';
0059     } else {
0060         return ch;
0061     }
0062 }
0063 
0064 char *kAsciiToLower(char *s)
0065 {
0066     if (!s) {
0067         return nullptr;
0068     }
0069     for (unsigned char *p = (unsigned char *) s; *p; ++p) {
0070         *p = ASCIIToLower(*p);
0071     }
0072     return s;
0073 }
0074 
0075 /** Convert a single ASCII character to uppercase.
0076     @param ch Character to convert
0077     @internal
0078 */
0079 static unsigned char ASCIIToUpper(unsigned char ch)
0080 {
0081     if (ch >= 'a' && ch <= 'z') {
0082         return ch - 'a' + 'A';
0083     } else {
0084         return ch;
0085     }
0086 }
0087 
0088 char *kAsciiToUpper(char *s)
0089 {
0090     if (!s) {
0091         return nullptr;
0092     }
0093     for (unsigned char *p = (unsigned char *) s; *p; ++p) {
0094         *p = ASCIIToUpper(*p);
0095     }
0096     return s;
0097 }
0098