summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-14 01:43:01 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-14 01:43:01 +0300
commit59e269cfec51b6a7933d6e847a38c84c04056675 (patch)
treed398b54d687d9cfce31d8ea5cdca2ba7dece354c
parent14de114ba811614ba6e058f6d864129f5c8b73bb (diff)
downloadmicropython-59e269cfec51b6a7933d6e847a38c84c04056675.tar.gz
micropython-59e269cfec51b6a7933d6e847a38c84c04056675.zip
qstr, objstr: Make sure that valid hash != 0, treat 0 as "not computed".
This feature was proposed with initial hashing RFC, and is prerequisite for seamless static str object definition.
-rw-r--r--py/objstr.c3
-rw-r--r--py/qstr.c7
2 files changed, 8 insertions, 2 deletions
diff --git a/py/objstr.c b/py/objstr.c
index d933fa5e34..0f9e4fdda4 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -1463,7 +1463,8 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
} else {
GET_STR_HASH(s1, h1);
GET_STR_HASH(s2, h2);
- if (h1 != h2) {
+ // If any of hashes is 0, it means it's not valid
+ if (h1 != 0 && h2 != 0 && h1 != h2) {
return false;
}
GET_STR_DATA_LEN(s1, d1, l1);
diff --git a/py/qstr.c b/py/qstr.c
index e4b5c111b5..990ff37727 100644
--- a/py/qstr.c
+++ b/py/qstr.c
@@ -35,7 +35,12 @@ machine_uint_t qstr_compute_hash(const byte *data, uint len) {
for (const byte *top = data + len; data < top; data++) {
hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
}
- return hash & 0xffff;
+ hash &= 0xffff;
+ // Make sure that valid hash is never zero, zero means "hash not computed"
+ if (hash == 0) {
+ hash++;
+ }
+ return hash;
}
typedef struct _qstr_pool_t {