hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

lookup.ha (896B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use hash::fnv;
      5 use strings;
      6 
      7 // Looks up a Media Type based on the mime type string, returning null if
      8 // unknown.
      9 export fn lookup_mime(mime: str) const nullable *mimetype = {
     10 	const hash = fnv::string(mime);
     11 	const bucket = &mimetable[hash % len(mimetable)];
     12 	for (let item .. *bucket) {
     13 		if (item.mime == mime) {
     14 			return item;
     15 		};
     16 	};
     17 	return null;
     18 };
     19 
     20 // Looks up a Media Type based on a file extension, with or without the leading
     21 // '.' character, returning null if unknown.
     22 export fn lookup_ext(ext: str) const nullable *mimetype = {
     23 	ext = strings::ltrim(ext, '.');
     24 	const hash = fnv::string(ext);
     25 	const bucket = &exttable[hash % len(exttable)];
     26 	for (let item .. *bucket) {
     27 		for (let j = 0z; j < len(item.exts); j += 1) {
     28 			if (item.exts[j] == ext) {
     29 				return item;
     30 			};
     31 		};
     32 	};
     33 	return null;
     34 };