1 | /********************************************************************** |
2 | * regex samples |
3 | * |
4 | * Copyright (c) 2019-2023 Dario Deledda. All rights reserved. |
5 | * Use of this source code is governed by an MIT license |
6 | * that can be found in the LICENSE file. |
7 | * |
8 | * This file contains a collection of regex samples |
9 | * |
10 | **********************************************************************/ |
11 | import regex |
12 | |
13 | /* |
14 | This simple function converts an HTML RGB value with 3 or 6 hex digits to a u32 value, |
15 | this function is not optimized and it is only for didatical purpose |
16 | example: #A0B0CC #A9F |
17 | */ |
18 | fn convert_html_rgb(in_col string) u32 { |
19 | mut n_digit := if in_col.len == 4 { 1 } else { 2 } |
20 | mut col_mul := if in_col.len == 4 { 4 } else { 0 } |
21 | |
22 | // this is the regex query, it uses V string interpolation to customize the regex query |
23 | // NOTE: if you want use escaped code you must use the r"" (raw) strings, |
24 | // *** please remember that V interpoaltion doesn't work on raw strings. *** |
25 | |
26 | query := '#([a-fA-F0-9]{${n_digit}})([a-fA-F0-9]{${n_digit}})([a-fA-F0-9]{${n_digit}})' |
27 | |
28 | mut re := regex.regex_opt(query) or { panic(err) } |
29 | start, end := re.match_string(in_col) |
30 | println('start: ${start}, end: ${end}') |
31 | mut res := u32(0) |
32 | if start >= 0 { |
33 | group_list := re.get_group_list() |
34 | r := ('0x' + in_col[group_list[0].start..group_list[0].end]).int() << col_mul |
35 | g := ('0x' + in_col[group_list[1].start..group_list[1].end]).int() << col_mul |
36 | b := ('0x' + in_col[group_list[2].start..group_list[2].end]).int() << col_mul |
37 | println('r: ${r} g: ${g} b: ${b}') |
38 | res = u32(r) << 16 | u32(g) << 8 | u32(b) |
39 | } |
40 | return res |
41 | } |
42 | |
43 | /* |
44 | This function demonstrates the use of the named groups |
45 | */ |
46 | fn convert_html_rgb_n(in_col string) u32 { |
47 | mut n_digit := if in_col.len == 4 { 1 } else { 2 } |
48 | mut col_mul := if in_col.len == 4 { 4 } else { 0 } |
49 | |
50 | query := '#(?P<red>[a-fA-F0-9]{${n_digit}})(?P<green>[a-fA-F0-9]{${n_digit}})(?P<blue>[a-fA-F0-9]{${n_digit}})' |
51 | |
52 | mut re := regex.regex_opt(query) or { panic(err) } |
53 | start, end := re.match_string(in_col) |
54 | println('start: ${start}, end: ${end}') |
55 | mut res := u32(0) |
56 | if start >= 0 { |
57 | red_s, red_e := re.get_group_bounds_by_name('red') |
58 | r := ('0x' + in_col[red_s..red_e]).int() << col_mul |
59 | |
60 | green_s, green_e := re.get_group_bounds_by_name('green') |
61 | g := ('0x' + in_col[green_s..green_e]).int() << col_mul |
62 | |
63 | blue_s, blue_e := re.get_group_bounds_by_name('blue') |
64 | b := ('0x' + in_col[blue_s..blue_e]).int() << col_mul |
65 | |
66 | println('r: ${r} g: ${g} b: ${b}') |
67 | res = u32(r) << 16 | u32(g) << 8 | u32(b) |
68 | } |
69 | return res |
70 | } |
71 | |
72 | fn main() { |
73 | // convert HTML rgb color using groups |
74 | println(convert_html_rgb('#A0b0Cc').hex()) |
75 | println(convert_html_rgb('#ABC').hex()) |
76 | |
77 | // convert HTML rgb color using named groups |
78 | println(convert_html_rgb_n('#A0B0CC').hex()) |
79 | println(convert_html_rgb_n('#ABC').hex()) |
80 | } |