From cb3e901be031f302fcad1ee90469d9931bc3df06 Mon Sep 17 00:00:00 2001 From: ugla Date: Fri, 22 Apr 2022 18:41:19 +0200 Subject: [PATCH] Bold match within name --- src/UnicodeData.txt | 2 +- src/main.zig | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/UnicodeData.txt b/src/UnicodeData.txt index b5abef7..25f9321 100644 --- a/src/UnicodeData.txt +++ b/src/UnicodeData.txt @@ -34623,4 +34623,4 @@ E01EF;VARIATION SELECTOR-256;Mn;0;NSM;;;;;N;;;;; F0000;;Co;0;L;;;;;N;;;;; FFFFD;;Co;0;L;;;;;N;;;;; 100000;;Co;0;L;;;;;N;;;;; -10FFFD;;Co;0;L;;;;;N;;;;; +10FFFD;;Co;0;L;;;;;N;;;;; \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 5c90dd5..6dfeec4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -22,24 +22,29 @@ pub fn main() anyerror!void { fn writeMatches(writer: anytype, needle: []const u8) !void { var split_it = std.mem.split(u8, UNICODE_DATA, "\n"); - while (split_it.next()) |line| { - if (std.mem.indexOf(u8, line, needle)) |_| { - try writeMatch(writer, line); - } - } + while (split_it.next()) |line| + try writeMatch(writer, line, needle); } -fn writeMatch(writer: anytype, line: []const u8) !void { +fn writeMatch(writer: anytype, line: []const u8, needle: []const u8) !void { var utf8_char: [8]u8 = undefined; var col_it = std.mem.split(u8, line, ";"); - const cp = try std.fmt.parseInt(u21, col_it.next() orelse return error.BongosData, 16); - const len = try std.unicode.utf8Encode(cp, &utf8_char); - try writer.print("{s} code: 0x{x:<6} name: {s:<14} category: {s}\n", .{ - utf8_char[0..len], - cp, - col_it.next() orelse return error.BongosData, - col_it.next() orelse return error.BongosData, - }); + const code = col_it.next() orelse return error.MissingCode; + const name = col_it.next() orelse return error.MissingName; + + if (std.mem.indexOf(u8, name, needle)) |idx| { + const category = col_it.next() orelse return error.MissingCategory; + const cp = try std.fmt.parseInt(u21, code, 16); + const len = try std.unicode.utf8Encode(cp, &utf8_char); + try writer.print("{s} code: 0x{x:<6} name: {s}\x1b[1m{s}\x1b[0m{s} category: {s}\n", .{ + utf8_char[0..len], + cp, + name[0..idx], + name[idx .. idx + needle.len], + name[idx + needle.len ..], + category, + }); + } } fn badToUpper(c: u8) u8 { -- 2.45.2