util.ha (1173B)
1 // SPDX-License-Identifier: GPL-3.0-only 2 // (c) Hare authors <https://harelang.org> 3 4 use ascii; 5 use dirs; 6 use hare::module; 7 use os; 8 use strings; 9 10 def HAREPATH: str = "."; 11 12 fn merge_tags(current: *[]str, new: str) (void | module::error) = { 13 let trimmed = strings::ltrim(new, '^'); 14 if (trimmed != new) { 15 free(*current); 16 *current = []; 17 }; 18 let newtags = module::parse_tags(trimmed)?; 19 defer free(newtags); 20 for :new (let newtag .. newtags) { 21 for (let i = 0z; i < len(current); i += 1) { 22 if (newtag.name == current[i]) { 23 if (!newtag.include) { 24 static delete(current[i]); 25 }; 26 continue :new; 27 }; 28 }; 29 if (newtag.include) { 30 append(current, newtag.name); 31 }; 32 }; 33 }; 34 35 fn harepath() str = os::tryenv("HAREPATH", HAREPATH); 36 37 fn harecache() str = { 38 match (os::getenv("HARECACHE")) { 39 case let s: str => 40 return s; 41 case void => 42 return dirs::cache("hare"); 43 }; 44 }; 45 46 // contents of slice shouldn't be freed 47 fn default_tags() []str = { 48 let arch = os::arch_name(os::architecture()); 49 static let platform: [7]u8 = [0...]; 50 let platform = ascii::strlower_buf(os::sysname(), platform[..0]); 51 let tags: []str = alloc([arch, platform]); 52 return tags; 53 };