Skip to content

Commit

Permalink
zig build: linkSystemLibrary integrates with pkg-config
Browse files Browse the repository at this point in the history
 * add -D CLI option for setting C macros
 * add std.ascii.allocLowerString
 * add std.ascii.eqlIgnoreCase
 * add std.ascii.indexOfIgnoreCasePos
 * add std.ascii.indexOfIgnoreCase
  • Loading branch information
andrewrk committed Sep 23, 2019
1 parent 35c1d8c commit 29b82d2
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
" --override-std-dir [arg] override path to Zig standard library\n"
" --override-lib-dir [arg] override path to Zig lib library\n"
" -ffunction-sections places each function in a separate section\n"
" -D[macro]=[value] define C [macro] to [value] (1 if [value] omitted)\n"
"\n"
"Link Options:\n"
" --bundle-compiler-rt for static libraries, include compiler-rt symbols\n"
Expand Down Expand Up @@ -691,6 +692,9 @@ int main(int argc, char **argv) {
bundle_compiler_rt = true;
} else if (strcmp(arg, "--test-cmd-bin") == 0) {
test_exec_args.append(nullptr);
} else if (arg[1] == 'D' && arg[2] != 0) {
clang_argv.append("-D");
clang_argv.append(&arg[2]);
} else if (arg[1] == 'L' && arg[2] != 0) {
// alias for --library-path
lib_dirs.append(&arg[2]);
Expand Down Expand Up @@ -769,6 +773,9 @@ int main(int argc, char **argv) {
dynamic_linker = buf_create_from_str(argv[i]);
} else if (strcmp(arg, "--libc") == 0) {
libc_txt = argv[i];
} else if (strcmp(arg, "-D") == 0) {
clang_argv.append("-D");
clang_argv.append(argv[i]);
} else if (strcmp(arg, "-isystem") == 0) {
clang_argv.append("-isystem");
clang_argv.append(argv[i]);
Expand Down
60 changes: 58 additions & 2 deletions std/ascii.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//
// https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/USASCII_code_chart.png/1200px-USASCII_code_chart.png

const std = @import("std");

const tIndex = enum(u3) {
Alpha,
Hex,
Expand All @@ -25,7 +27,6 @@ const tIndex = enum(u3) {
const combinedTable = init: {
comptime var table: [256]u8 = undefined;

const std = @import("std");
const mem = std.mem;

const alpha = [_]u1{
Expand Down Expand Up @@ -215,7 +216,6 @@ pub fn toLower(c: u8) u8 {
}

test "ascii character classes" {
const std = @import("std");
const testing = std.testing;

testing.expect('C' == toUpper('c'));
Expand All @@ -226,3 +226,59 @@ test "ascii character classes" {
testing.expect(!isAlpha('5'));
testing.expect(isSpace(' '));
}

pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {
const result = try allocator.alloc(u8, ascii_string.len);
for (result) |*c, i| {
c.* = toLower(ascii_string[i]);
}
return result;
}

test "allocLowerString" {
var buf: [100]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
const result = try allocLowerString(allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));
}

pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
for (a) |a_c, i| {
if (toLower(a_c) != toLower(b[i])) return false;
}
return true;
}

test "eqlIgnoreCase" {
std.testing.expect(eqlIgnoreCase("HEl💩Lo!", "hel💩lo!"));
std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
}

/// Finds `substr` in `container`, starting at `start_index`.
/// TODO boyer-moore algorithm
pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
if (substr.len > container.len) return null;

var i: usize = start_index;
const end = container.len - substr.len;
while (i <= end) : (i += 1) {
if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i;
}
return null;
}

/// Finds `substr` in `container`, starting at `start_index`.
pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
return indexOfIgnoreCasePos(container, 0, substr);
}

test "indexOfIgnoreCase" {
std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
std.testing.expect(indexOfIgnoreCase("foo", "fool") == null);

std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
}
Loading

0 comments on commit 29b82d2

Please sign in to comment.