zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

bcmp.zig (874B) - Raw


      1 const std = @import("std");
      2 const common = @import("./common.zig");
      3 
      4 comptime {
      5     @export(&bcmp, .{ .name = "bcmp", .linkage = common.linkage, .visibility = common.visibility });
      6 }
      7 
      8 pub fn bcmp(vl: [*]allowzero const u8, vr: [*]allowzero const u8, n: usize) callconv(.c) c_int {
      9     @setRuntimeSafety(false);
     10 
     11     var index: usize = 0;
     12     while (index != n) : (index += 1) {
     13         if (vl[index] != vr[index]) {
     14             return 1;
     15         }
     16     }
     17 
     18     return 0;
     19 }
     20 
     21 test "bcmp" {
     22     const base_arr = &[_]u8{ 1, 1, 1 };
     23     const arr1 = &[_]u8{ 1, 1, 1 };
     24     const arr2 = &[_]u8{ 1, 0, 1 };
     25     const arr3 = &[_]u8{ 1, 2, 1 };
     26 
     27     try std.testing.expect(bcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
     28     try std.testing.expect(bcmp(base_arr[0..], arr2[0..], base_arr.len) != 0);
     29     try std.testing.expect(bcmp(base_arr[0..], arr3[0..], base_arr.len) != 0);
     30 }