DB-GPT/dbgpt/agent/util/cmp.py
2024-04-10 22:44:53 +08:00

31 lines
850 B
Python

"""Compare string utility functions."""
import string
def cmp_string_equal(
a: str,
b: str,
ignore_case: bool = False,
ignore_punctuation: bool = False,
ignore_whitespace: bool = False,
) -> bool:
"""Compare two strings are equal or not.
Args:
a(str): The first string.
b(str): The second string.
ignore_case(bool): Ignore case or not.
ignore_punctuation(bool): Ignore punctuation or not.
ignore_whitespace(bool): Ignore whitespace or not.
"""
if ignore_case:
a = a.lower()
b = b.lower()
if ignore_punctuation:
a = a.translate(str.maketrans("", "", string.punctuation))
b = b.translate(str.maketrans("", "", string.punctuation))
if ignore_whitespace:
a = "".join(a.split())
b = "".join(b.split())
return a == b