Skip to content

Commit a6ff708

Browse files
committed
Add a built-in hash function
hash constant is widely used in file verification, so I suggest adding a built-in `hash()` function as a basic function. For its usage example: ``` hash_str = hash('foobar', 'sha1') # return a sha1 constant # some process ```
1 parent 1e7f6f0 commit a6ff708

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

data/syntax-highlighting/vim/syntax/meson.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ syn keyword mesonBuiltin
109109
\ jar
110110
\ join_paths
111111
\ library
112+
\ hash
112113
\ message
113114
\ project
114115
\ range

docs/yaml/functions/hash.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: hash
2+
returns: str
3+
description: |
4+
Run the specified algorithm to calculate the hash constant
5+
of a given string.
6+
7+
example: |
8+
After being called, it will return the hash constant calculated
9+
using the specified algorithm.
10+
11+
```meson
12+
message(hash('foobar', 'md5')) # 3858f62230ac3c915f300c664312c63f
13+
message(hash('foobar', 'sha1')) # 8843d7f92416211de9ebb963ff4ce28125932878
14+
message(hash('foobar', 'sha256')) # c3ab8ff13720e8ad9047dd39466b3c8974e592c2f
15+
a383d4a3960714caef0c4f2
16+
...
17+
```
18+
19+
posargs:
20+
string:
21+
type: str
22+
description: A string constant or variable.
23+
24+
hash_type:
25+
type: str
26+
description: |
27+
It supports the following algorithms:
28+
29+
- md5
30+
- sha1
31+
- sha224
32+
- sha256
33+
- sha384
34+
- sha512
35+
- sha3_224
36+
- sha3_256
37+
- sha3_384
38+
- sha3_512

mesonbuild/ast/interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ def __init__(self, source_root: str, subdir: str, subproject: SubProject, subpro
247247
'range': self.func_do_nothing,
248248
'structured_sources': self.func_do_nothing,
249249
'debug': self.func_do_nothing,
250+
'hash': self.func_do_nothing,
250251
})
251252

252253
def _unholder_args(self, args: T.Any, kwargs: T.Any) -> T.Tuple[T.Any, T.Any]:

mesonbuild/interpreter/interpreter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def build_func_dict(self) -> None:
404404
'unset_variable': self.func_unset_variable,
405405
'vcs_tag': self.func_vcs_tag,
406406
'warning': self.func_warning,
407+
'hash': self.func_hash,
407408
})
408409
if 'MESON_UNIT_TEST' in os.environ:
409410
self.funcs.update({'exception': self.func_exception})
@@ -1430,6 +1431,21 @@ def compatibility_sort_helper(s):
14301431
if main_summary:
14311432
main_summary.dump()
14321433

1434+
@typed_pos_args('hash', varargs=str, min_varargs=2 ,max_varargs=2)
1435+
@noKwargs
1436+
def func_hash(self, node: mparser.BaseNode, args: T.Tuple[T.List[str]], kwargs: 'TYPE_kwargs') -> str:
1437+
from hashlib import new
1438+
string, hash_type = args[0][0:2]
1439+
# For supported algorithms
1440+
# see https://docs.python.org/3/library/hashlib.html#hash-objects
1441+
if hash_type not in ['md5', 'sha1',
1442+
'sha224', 'sha256', 'sha384', 'sha512',
1443+
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512']:
1444+
raise InterpreterException(f'{hash_type} is not supported :(')
1445+
hash_obj = new(hash_type)
1446+
hash_obj.update(string.encode())
1447+
return hash_obj.hexdigest()
1448+
14331449
@noArgsFlattening
14341450
@FeatureNew('warning', '0.44.0')
14351451
@noKwargs

0 commit comments

Comments
 (0)