1# Copyright 2020 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""This module defines the string to ID hash used in pw_rpc.""" 15 16HASH_CONSTANT = 65599 17 18 19# This is the same hash function that is used in pw_tokenizer, with the maximum 20# length removed. It is chosen due to its simplicity. The tokenizer code is 21# duplicated here to avoid unnecessary dependencies between modules. 22def hash_65599(string: str) -> int: 23 hash_value = len(string) 24 coefficient = HASH_CONSTANT 25 26 for char in string: 27 hash_value = (hash_value + coefficient * ord(char)) % 2**32 28 coefficient = (coefficient * HASH_CONSTANT) % 2**32 29 30 return hash_value 31 32 33# Function that converts a name to an ID. 34calculate = hash_65599 35