use the zig formatter

This commit is contained in:
Folkert 2020-12-01 21:26:37 +01:00
parent 92544e5b07
commit 1589e688b3
4 changed files with 6995 additions and 7069 deletions

View file

@ -38,7 +38,7 @@ pub fn build(b: *Builder) void {
const ir_to_bitcode = b.addSystemCommand(&[_][]const u8{ const ir_to_bitcode = b.addSystemCommand(&[_][]const u8{
"llvm-as-10", "llvm-as-10",
ir_out_file, ir_out_file,
bitcode_path_arg bitcode_path_arg,
}); });
const bicode = b.step("bc", "Build LLVM ir and convert to bitcode"); const bicode = b.step("bc", "Build LLVM ir and convert to bitcode");

View file

@ -39,7 +39,7 @@ pub const BoundClass = enum(u8) {
// grapheme-boundary rules, so we store it in the boundclass field // grapheme-boundary rules, so we store it in the boundclass field
EXTENDED_PICTOGRAPHIC = 19, EXTENDED_PICTOGRAPHIC = 19,
E_ZWG = 20, // BoundClass.EXTENDED_PICTOGRAPHIC + ZWJ E_ZWG = 20, // BoundClass.EXTENDED_PICTOGRAPHIC + ZWJ
} ; };
test "Bound Class" { test "Bound Class" {
expectEqual(0, @enumToInt(BoundClass.START)); expectEqual(0, @enumToInt(BoundClass.START));
@ -49,8 +49,7 @@ test "Bound Class" {
fn graphemeBreakSimple(lbc: BoundClass, tbc: BoundClass) bool { fn graphemeBreakSimple(lbc: BoundClass, tbc: BoundClass) bool {
const lbc_u8 = @enumToInt(lbc); const lbc_u8 = @enumToInt(lbc);
const tbc_u8 = @enumToInt(tbc); const tbc_u8 = @enumToInt(tbc);
return ( return (if (lbc_u8 == @enumToInt(BoundClass.START)) // GB1
if (lbc_u8 == @enumToInt(BoundClass.START)) // GB1
true true
else if (lbc_u8 == @enumToInt(BoundClass.CR) and tbc_u8 == @enumToInt(BoundClass.LF)) // GB3 else if (lbc_u8 == @enumToInt(BoundClass.CR) and tbc_u8 == @enumToInt(BoundClass.LF)) // GB3
false false
@ -71,8 +70,7 @@ fn graphemeBreakSimple(lbc: BoundClass, tbc: BoundClass) bool {
else if (lbc_u8 == @enumToInt(BoundClass.REGIONAL_INDICATOR) and tbc_u8 == @enumToInt(BoundClass.REGIONAL_INDICATOR)) // GB12/13 (requires additional handling below) else if (lbc_u8 == @enumToInt(BoundClass.REGIONAL_INDICATOR) and tbc_u8 == @enumToInt(BoundClass.REGIONAL_INDICATOR)) // GB12/13 (requires additional handling below)
false false
else // GB999 else // GB999
true true);
);
} }
// https://github.com/JuliaStrings/utf8proc/blob/master/utf8proc.c#L291 // https://github.com/JuliaStrings/utf8proc/blob/master/utf8proc.c#L291
@ -151,11 +149,7 @@ test "codepointToBoundClass: invalid" {
// which require this state will not be applied, essentially matching the rules in // which require this state will not be applied, essentially matching the rules in
// Unicode 8.0.0. // Unicode 8.0.0.
pub fn isGraphemeBreak(codePoint1: u21, codePoint2: u21, opt_state_ptr: *?BoundClass) bool { pub fn isGraphemeBreak(codePoint1: u21, codePoint2: u21, opt_state_ptr: *?BoundClass) bool {
return graphemeBreakExtended( return graphemeBreakExtended(codepointToBoundClass(codePoint1).*, codepointToBoundClass(codePoint2).*, opt_state_ptr);
codepointToBoundClass(codePoint1).*,
codepointToBoundClass(codePoint2).*,
opt_state_ptr
);
} }
// https://github.com/JuliaStrings/utf8proc/blob/master/utf8proc_data.c // https://github.com/JuliaStrings/utf8proc/blob/master/utf8proc_data.c

View file

@ -4,18 +4,22 @@ const testing = std.testing;
// Num Module // Num Module
const num = @import("num.zig"); const num = @import("num.zig");
comptime { exportNumFn(num.atan, "atan"); } comptime {
comptime { exportNumFn(num.isFinite, "is_finite"); } exportNumFn(num.atan, "atan");
comptime { exportNumFn(num.powInt, "pow_int"); } exportNumFn(num.isFinite, "is_finite");
comptime { exportNumFn(num.acos, "acos"); } exportNumFn(num.powInt, "pow_int");
comptime { exportNumFn(num.asin, "asin"); } exportNumFn(num.acos, "acos");
exportNumFn(num.asin, "asin");
}
// Str Module // Str Module
const str = @import("str.zig"); const str = @import("str.zig");
comptime { exportStrFn(str.strSplitInPlace, "str_split_in_place"); } comptime {
comptime { exportStrFn(str.countSegments, "count_segments"); } exportStrFn(str.strSplitInPlace, "str_split_in_place");
comptime { exportStrFn(str.countGraphemeClusters, "count_grapheme_clusters"); } exportStrFn(str.countSegments, "count_segments");
comptime { exportStrFn(str.startsWith, "starts_with"); } exportStrFn(str.countGraphemeClusters, "count_grapheme_clusters");
exportStrFn(str.startsWith, "starts_with");
}
// Export helpers - Must be run inside a comptime // Export helpers - Must be run inside a comptime
fn exportBuiltinFn(comptime fn_target: anytype, comptime fn_name: []const u8) void { fn exportBuiltinFn(comptime fn_target: anytype, comptime fn_name: []const u8) void {

View file

@ -12,9 +12,9 @@ const RocStr = struct {
str_len: usize, str_len: usize,
pub fn empty() RocStr { pub fn empty() RocStr {
return RocStr { return RocStr{
.str_len = 0, .str_len = 0,
.str_bytes = null .str_bytes = null,
}; };
} }
@ -26,7 +26,7 @@ const RocStr = struct {
if (length < rocStrSize) { if (length < rocStrSize) {
var ret_small_str = RocStr.empty(); var ret_small_str = RocStr.empty();
const target_ptr = @ptrToInt(&ret_small_str); const target_ptr = @ptrToInt(&ret_small_str);
var index : u8 = 0; var index: u8 = 0;
// Zero out the data, just to be safe // Zero out the data, just to be safe
while (index < rocStrSize) { while (index < rocStrSize) {
var offset_ptr = @intToPtr(*u8, target_ptr + index); var offset_ptr = @intToPtr(*u8, target_ptr + index);
@ -51,9 +51,9 @@ const RocStr = struct {
@memcpy(new_bytes, bytes, length); @memcpy(new_bytes, bytes, length);
return RocStr { return RocStr{
.str_bytes = new_bytes, .str_bytes = new_bytes,
.str_len = length .str_len = length,
}; };
} }
} }
@ -199,22 +199,15 @@ const RocStr = struct {
// Str.split // Str.split
pub fn strSplitInPlace( pub fn strSplitInPlace(array: [*]RocStr, array_len: usize, str_bytes: [*]const u8, str_len: usize, delimiter_bytes_ptrs: [*]const u8, delimiter_len: usize) callconv(.C) void {
array: [*]RocStr, var ret_array_index: usize = 0;
array_len: usize, var sliceStart_index: usize = 0;
str_bytes: [*]const u8, var str_index: usize = 0;
str_len: usize,
delimiter_bytes_ptrs: [*]const u8,
delimiter_len: usize
) callconv(.C) void {
var ret_array_index : usize = 0;
var sliceStart_index : usize = 0;
var str_index : usize = 0;
if (str_len > delimiter_len) { if (str_len > delimiter_len) {
const end_index : usize = str_len - delimiter_len + 1; const end_index: usize = str_len - delimiter_len + 1;
while (str_index <= end_index) { while (str_index <= end_index) {
var delimiter_index : usize = 0; var delimiter_index: usize = 0;
var matches_delimiter = true; var matches_delimiter = true;
while (delimiter_index < delimiter_len) { while (delimiter_index < delimiter_len) {
@ -230,7 +223,7 @@ pub fn strSplitInPlace(
} }
if (matches_delimiter) { if (matches_delimiter) {
const segment_len : usize = str_index - sliceStart_index; const segment_len: usize = str_index - sliceStart_index;
array[ret_array_index] = RocStr.init(str_bytes + sliceStart_index, segment_len); array[ret_array_index] = RocStr.init(str_bytes + sliceStart_index, segment_len);
sliceStart_index = str_index + delimiter_len; sliceStart_index = str_index + delimiter_len;
@ -247,7 +240,6 @@ pub fn strSplitInPlace(
test "strSplitInPlace: no delimiter" { test "strSplitInPlace: no delimiter" {
// Str.split "abc" "!" == [ "abc" ] // Str.split "abc" "!" == [ "abc" ]
var str: [3]u8 = "abc".*; var str: [3]u8 = "abc".*;
const str_ptr: [*]const u8 = &str; const str_ptr: [*]const u8 = &str;
@ -257,14 +249,7 @@ test "strSplitInPlace: no delimiter" {
var array: [1]RocStr = undefined; var array: [1]RocStr = undefined;
const array_ptr: [*]RocStr = &array; const array_ptr: [*]RocStr = &array;
strSplitInPlace( strSplitInPlace(array_ptr, 1, str_ptr, 3, delimiter_ptr, 1);
array_ptr,
1,
str_ptr,
3,
delimiter_ptr,
1
);
var expected = [1]RocStr{ var expected = [1]RocStr{
RocStr.init(str_ptr, 3), RocStr.init(str_ptr, 3),
@ -292,22 +277,15 @@ test "strSplitInPlace: empty end" {
const delimiter: [delimiter_len:0]u8 = "---- ---- ---- ---- ----".*; const delimiter: [delimiter_len:0]u8 = "---- ---- ---- ---- ----".*;
const delimiter_ptr: [*]const u8 = &delimiter; const delimiter_ptr: [*]const u8 = &delimiter;
const array_len : usize = 3; const array_len: usize = 3;
var array: [array_len]RocStr = [_]RocStr { var array: [array_len]RocStr = [_]RocStr{
undefined, undefined,
undefined, undefined,
undefined, undefined,
}; };
const array_ptr: [*]RocStr = &array; const array_ptr: [*]RocStr = &array;
strSplitInPlace( strSplitInPlace(array_ptr, array_len, str_ptr, str_len, delimiter_ptr, delimiter_len);
array_ptr,
array_len,
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
const first_expected_str_len: usize = 1; const first_expected_str_len: usize = 1;
var first_expected_str: [first_expected_str_len]u8 = "1".*; var first_expected_str: [first_expected_str_len]u8 = "1".*;
@ -329,7 +307,6 @@ test "strSplitInPlace: empty end" {
test "strSplitInPlace: delimiter on sides" { test "strSplitInPlace: delimiter on sides" {
// Str.split "tttghittt" "ttt" == [ "", "ghi", "" ] // Str.split "tttghittt" "ttt" == [ "", "ghi", "" ]
const str_len: usize = 9; const str_len: usize = 9;
var str: [str_len]u8 = "tttghittt".*; var str: [str_len]u8 = "tttghittt".*;
const str_ptr: [*]u8 = &str; const str_ptr: [*]u8 = &str;
@ -338,22 +315,15 @@ test "strSplitInPlace: delimiter on sides" {
var delimiter: [delimiter_len]u8 = "ttt".*; var delimiter: [delimiter_len]u8 = "ttt".*;
const delimiter_ptr: [*]u8 = &delimiter; const delimiter_ptr: [*]u8 = &delimiter;
const array_len : usize = 3; const array_len: usize = 3;
var array: [array_len]RocStr = [_]RocStr{ var array: [array_len]RocStr = [_]RocStr{
undefined , undefined,
undefined, undefined,
undefined, undefined,
}; };
const array_ptr: [*]RocStr = &array; const array_ptr: [*]RocStr = &array;
strSplitInPlace( strSplitInPlace(array_ptr, array_len, str_ptr, str_len, delimiter_ptr, delimiter_len);
array_ptr,
array_len,
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
const expected_str_len: usize = 3; const expected_str_len: usize = 3;
var expected_str: [expected_str_len]u8 = "ghi".*; var expected_str: [expected_str_len]u8 = "ghi".*;
@ -369,7 +339,6 @@ test "strSplitInPlace: delimiter on sides" {
test "strSplitInPlace: three pieces" { test "strSplitInPlace: three pieces" {
// Str.split "a!b!c" "!" == [ "a", "b", "c" ] // Str.split "a!b!c" "!" == [ "a", "b", "c" ]
const str_len: usize = 5; const str_len: usize = 5;
var str: [str_len]u8 = "a!b!c".*; var str: [str_len]u8 = "a!b!c".*;
const str_ptr: [*]u8 = &str; const str_ptr: [*]u8 = &str;
@ -378,18 +347,11 @@ test "strSplitInPlace: three pieces" {
var delimiter: [delimiter_len]u8 = "!".*; var delimiter: [delimiter_len]u8 = "!".*;
const delimiter_ptr: [*]u8 = &delimiter; const delimiter_ptr: [*]u8 = &delimiter;
const array_len : usize = 3; const array_len: usize = 3;
var array: [array_len]RocStr = undefined; var array: [array_len]RocStr = undefined;
const array_ptr: [*]RocStr = &array; const array_ptr: [*]RocStr = &array;
strSplitInPlace( strSplitInPlace(array_ptr, array_len, str_ptr, str_len, delimiter_ptr, delimiter_len);
array_ptr,
array_len,
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
var a: [1]u8 = "a".*; var a: [1]u8 = "a".*;
const a_ptr: [*]u8 = &a; const a_ptr: [*]u8 = &a;
@ -412,7 +374,7 @@ test "strSplitInPlace: three pieces" {
RocStr{ RocStr{
.str_bytes = c_ptr, .str_bytes = c_ptr,
.str_len = 1, .str_len = 1,
} },
}; };
// TODO: fix those tests // TODO: fix those tests
@ -426,12 +388,7 @@ test "strSplitInPlace: three pieces" {
// It is used to count how many segments the input `_str` // It is used to count how many segments the input `_str`
// needs to be broken into, so that we can allocate a array // needs to be broken into, so that we can allocate a array
// of that size. It always returns at least 1. // of that size. It always returns at least 1.
pub fn countSegments( pub fn countSegments(str_bytes: [*]u8, str_len: usize, delimiter_bytes_ptrs: [*]u8, delimiter_len: usize) callconv(.C) usize {
str_bytes: [*]u8,
str_len: usize,
delimiter_bytes_ptrs: [*]u8,
delimiter_len: usize
) callconv(.C) usize {
var count: usize = 1; var count: usize = 1;
if (str_len > delimiter_len) { if (str_len > delimiter_len) {
@ -469,7 +426,6 @@ pub fn countSegments(
test "countSegments: long delimiter" { test "countSegments: long delimiter" {
// Str.split "str" "delimiter" == [ "str" ] // Str.split "str" "delimiter" == [ "str" ]
// 1 segment // 1 segment
const str_len: usize = 3; const str_len: usize = 3;
var str: [str_len]u8 = "str".*; var str: [str_len]u8 = "str".*;
const str_ptr: [*]u8 = &str; const str_ptr: [*]u8 = &str;
@ -478,12 +434,7 @@ test "countSegments: long delimiter" {
var delimiter: [delimiter_len]u8 = "delimiter".*; var delimiter: [delimiter_len]u8 = "delimiter".*;
const delimiter_ptr: [*]u8 = &delimiter; const delimiter_ptr: [*]u8 = &delimiter;
const segments_count = countSegments( const segments_count = countSegments(str_ptr, str_len, delimiter_ptr, delimiter_len);
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
expectEqual(segments_count, 1); expectEqual(segments_count, 1);
} }
@ -491,7 +442,6 @@ test "countSegments: long delimiter" {
test "countSegments: delimiter at start" { test "countSegments: delimiter at start" {
// Str.split "hello there" "hello" == [ "", " there" ] // Str.split "hello there" "hello" == [ "", " there" ]
// 2 segments // 2 segments
const str_len: usize = 11; const str_len: usize = 11;
var str: [str_len]u8 = "hello there".*; var str: [str_len]u8 = "hello there".*;
const str_ptr: [*]u8 = &str; const str_ptr: [*]u8 = &str;
@ -500,12 +450,7 @@ test "countSegments: delimiter at start" {
var delimiter: [delimiter_len]u8 = "hello".*; var delimiter: [delimiter_len]u8 = "hello".*;
const delimiter_ptr: [*]u8 = &delimiter; const delimiter_ptr: [*]u8 = &delimiter;
const segments_count = countSegments( const segments_count = countSegments(str_ptr, str_len, delimiter_ptr, delimiter_len);
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
expectEqual(segments_count, 2); expectEqual(segments_count, 2);
} }
@ -513,7 +458,6 @@ test "countSegments: delimiter at start" {
test "countSegments: delimiter interspered" { test "countSegments: delimiter interspered" {
// Str.split "a!b!c" "!" == [ "a", "b", "c" ] // Str.split "a!b!c" "!" == [ "a", "b", "c" ]
// 3 segments // 3 segments
const str_len: usize = 5; const str_len: usize = 5;
var str: [str_len]u8 = "a!b!c".*; var str: [str_len]u8 = "a!b!c".*;
const str_ptr: [*]u8 = &str; const str_ptr: [*]u8 = &str;
@ -522,12 +466,7 @@ test "countSegments: delimiter interspered" {
var delimiter: [delimiter_len]u8 = "!".*; var delimiter: [delimiter_len]u8 = "!".*;
const delimiter_ptr: [*]u8 = &delimiter; const delimiter_ptr: [*]u8 = &delimiter;
const segments_count = countSegments( const segments_count = countSegments(str_ptr, str_len, delimiter_ptr, delimiter_len);
str_ptr,
str_len,
delimiter_ptr,
delimiter_len
);
expectEqual(segments_count, 3); expectEqual(segments_count, 3);
} }
@ -545,11 +484,7 @@ pub fn countGraphemeClusters(bytes_ptr: [*]u8, bytes_len: usize) callconv(.C) u
var opt_last_codepoint: ?u21 = null; var opt_last_codepoint: ?u21 = null;
while (iter.nextCodepoint()) |cur_codepoint| { while (iter.nextCodepoint()) |cur_codepoint| {
if (opt_last_codepoint) |last_codepoint| { if (opt_last_codepoint) |last_codepoint| {
var did_break = grapheme.isGraphemeBreak( var did_break = grapheme.isGraphemeBreak(last_codepoint, cur_codepoint, grapheme_break_state_ptr);
last_codepoint,
cur_codepoint,
grapheme_break_state_ptr
);
if (did_break) { if (did_break) {
count += 1; count += 1;
grapheme_break_state = null; grapheme_break_state = null;
@ -615,23 +550,17 @@ test "countGraphemeClusters: emojis, ut8, and ascii characters" {
expectEqual(count, 10); expectEqual(count, 10);
} }
// Str.startsWith // Str.startsWith
pub fn startsWith( pub fn startsWith(bytes_ptr: [*]u8, bytes_len: usize, prefix_ptr: [*]u8, prefix_len: usize) callconv(.C) bool {
bytes_ptr: [*]u8, if (prefix_len > bytes_len) {
bytes_len: usize,
prefix_ptr: [*]u8,
prefix_len: usize
) callconv(.C) bool {
if(prefix_len > bytes_len) {
return false; return false;
} }
// we won't exceed bytes_len due to the previous check // we won't exceed bytes_len due to the previous check
var i : usize = 0; var i: usize = 0;
while(i < prefix_len) { while (i < prefix_len) {
if(bytes_ptr[i] != prefix_ptr[i]) { if (bytes_ptr[i] != prefix_ptr[i]) {
return false; return false;
} }
i += 1; i += 1;
@ -639,7 +568,6 @@ pub fn startsWith(
return true; return true;
} }
test "startsWith: 123456789123456789 starts with 123456789123456789" { test "startsWith: 123456789123456789 starts with 123456789123456789" {
const str_len: usize = 18; const str_len: usize = 18;
var str: [str_len]u8 = "123456789123456789".*; var str: [str_len]u8 = "123456789123456789".*;