File: pm_strncasecmp.c

package info (click to toggle)
ruby3.4 3.4.5-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 154,784 kB
  • sloc: ruby: 1,259,653; ansic: 829,955; yacc: 28,233; pascal: 7,359; sh: 3,864; python: 1,799; cpp: 1,158; asm: 808; makefile: 801; javascript: 414; lisp: 109; perl: 62; awk: 36; sed: 4; xml: 4
file content (36 lines) | stat: -rw-r--r-- 1,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "prism/util/pm_strncasecmp.h"

/**
 * A locale-insensitive version of `tolower(3)`
 */
static inline int
pm_tolower(int c)
{
    if ('A' <= c && c <= 'Z') {
        return c | 0x20;
    }
    return c;
}

/**
 * Compare two strings, ignoring case, up to the given length. Returns 0 if the
 * strings are equal, a negative number if string1 is less than string2, or a
 * positive number if string1 is greater than string2.
 *
 * Note that this is effectively our own implementation of strncasecmp, but it's
 * not available on all of the platforms we want to support so we're rolling it
 * here.
 */
int
pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) {
    size_t offset = 0;
    int difference = 0;

    while (offset < length && string1[offset] != '\0') {
        if (string2[offset] == '\0') return string1[offset];
        if ((difference = pm_tolower(string1[offset]) - pm_tolower(string2[offset])) != 0) return difference;
        offset++;
    }

    return difference;
}