master
  1const builtin = @import("builtin");
  2const std = @import("std");
  3const common = @import("common.zig");
  4
  5comptime {
  6    @export(&strcmp, .{ .name = "strcmp", .linkage = common.linkage, .visibility = common.visibility });
  7    @export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility });
  8    @export(&strcasecmp, .{ .name = "strcasecmp", .linkage = common.linkage, .visibility = common.visibility });
  9    @export(&strncasecmp, .{ .name = "strncasecmp", .linkage = common.linkage, .visibility = common.visibility });
 10    @export(&__strcasecmp_l, .{ .name = "__strcasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
 11    @export(&__strncasecmp_l, .{ .name = "__strncasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
 12    @export(&__strcasecmp_l, .{ .name = "strcasecmp_l", .linkage = .weak, .visibility = common.visibility });
 13    @export(&__strncasecmp_l, .{ .name = "strncasecmp_l", .linkage = .weak, .visibility = common.visibility });
 14}
 15
 16fn strcmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
 17    // We need to perform unsigned comparisons.
 18    return switch (std.mem.orderZ(u8, @ptrCast(s1), @ptrCast(s2))) {
 19        .lt => -1,
 20        .eq => 0,
 21        .gt => 1,
 22    };
 23}
 24
 25fn strncmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int {
 26    if (n == 0) return 0;
 27
 28    var l: [*:0]const u8 = @ptrCast(s1);
 29    var r: [*:0]const u8 = @ptrCast(s2);
 30    var i = n - 1;
 31
 32    while (l[0] != 0 and r[0] != 0 and i != 0 and l[0] == r[0]) {
 33        l += 1;
 34        r += 1;
 35        i -= 1;
 36    }
 37
 38    return @as(c_int, l[0]) - @as(c_int, r[0]);
 39}
 40
 41fn strcasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
 42    const toLower = std.ascii.toLower;
 43    var l: [*:0]const u8 = @ptrCast(s1);
 44    var r: [*:0]const u8 = @ptrCast(s2);
 45
 46    while (l[0] != 0 and r[0] != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) {
 47        l += 1;
 48        r += 1;
 49    }
 50
 51    return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
 52}
 53
 54fn __strcasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, locale: *anyopaque) callconv(.c) c_int {
 55    _ = locale;
 56    return strcasecmp(s1, s2);
 57}
 58
 59fn strncasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int {
 60    const toLower = std.ascii.toLower;
 61    var l: [*:0]const u8 = @ptrCast(s1);
 62    var r: [*:0]const u8 = @ptrCast(s2);
 63    var i = n - 1;
 64
 65    while (l[0] != 0 and r[0] != 0 and i != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) {
 66        l += 1;
 67        r += 1;
 68        i -= 1;
 69    }
 70
 71    return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
 72}
 73
 74fn __strncasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize, locale: *anyopaque) callconv(.c) c_int {
 75    _ = locale;
 76    return strncasecmp(s1, s2, n);
 77}
 78
 79test strcasecmp {
 80    try std.testing.expect(strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0);
 81    try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0);
 82    try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0);
 83    try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("A")) > 0);
 84    try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("A")) == 0);
 85    try std.testing.expect(strcasecmp(@ptrCast("B"), @ptrCast("b")) == 0);
 86    try std.testing.expect(strcasecmp(@ptrCast("bb"), @ptrCast("AA")) > 0);
 87}
 88
 89test strncasecmp {
 90    try std.testing.expect(strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
 91    try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
 92    try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0);
 93    try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("A"), 1) > 0);
 94    try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("A"), 1) == 0);
 95    try std.testing.expect(strncasecmp(@ptrCast("B"), @ptrCast("b"), 1) == 0);
 96    try std.testing.expect(strncasecmp(@ptrCast("bb"), @ptrCast("AA"), 2) > 0);
 97}
 98
 99test strncmp {
100    try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
101    try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0);
102    try std.testing.expect(strncmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
103    try std.testing.expect(strncmp(@ptrCast("\xff"), @ptrCast("\x02"), 1) > 0);
104}