macos/foundation: more string funcs

This commit is contained in:
Mitchell Hashimoto
2022-10-01 10:32:54 -07:00
parent 2440b0ec67
commit 92d564a246
3 changed files with 39 additions and 2 deletions

View File

@ -1,4 +1,5 @@
pub usingnamespace @import("foundation/array.zig");
pub usingnamespace @import("foundation/base.zig");
pub usingnamespace @import("foundation/dictionary.zig");
pub usingnamespace @import("foundation/string.zig");
pub usingnamespace @import("foundation/type.zig");

View File

@ -0,0 +1,5 @@
pub const ComparisonResult = enum(c_int) {
less = -1,
equal = 0,
greater = 1,
};

View File

@ -1,6 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const cftype = @import("type.zig");
const foundation = @import("../foundation.zig");
pub const String = opaque {
pub fn createWithBytes(
@ -18,13 +18,24 @@ pub const String = opaque {
}
pub fn release(self: *String) void {
cftype.CFRelease(self);
foundation.CFRelease(self);
}
pub fn hasPrefix(self: *String, prefix: *String) bool {
return CFStringHasPrefix(self, prefix) == 1;
}
pub fn compare(
self: *String,
other: *String,
options: StringComparison,
) foundation.ComparisonResult {
return @intToEnum(
foundation.ComparisonResult,
CFStringCompare(self, other, @bitCast(c_int, options)),
);
}
pub extern "c" fn CFStringCreateWithBytes(
allocator: ?*anyopaque,
bytes: [*]const u8,
@ -33,6 +44,25 @@ pub const String = opaque {
is_external: bool,
) ?*String;
pub extern "c" fn CFStringHasPrefix(*String, *String) u8;
pub extern "c" fn CFStringCompare(*String, *String, c_int) c_int;
};
pub const StringComparison = packed struct {
case_insensitive: bool = false,
_unused_2: bool = false,
backwards: bool = false,
anchored: bool = false,
nonliteral: bool = false,
localized: bool = false,
numerically: bool = false,
diacritic_insensitive: bool = false,
width_insensitive: bool = false,
forced_ordering: bool = false,
_padding: u22 = 0,
test {
try std.testing.expectEqual(@bitSizeOf(c_int), @bitSizeOf(StringComparison));
}
};
/// https://developer.apple.com/documentation/corefoundation/cfstringencoding?language=objc
@ -63,4 +93,5 @@ test "string" {
defer prefix.release();
try testing.expect(str.hasPrefix(prefix));
try testing.expectEqual(foundation.ComparisonResult.equal, str.compare(str, .{}));
}