2022-08-19 15:14:38 -04:00
|
|
|
# Copyright 2022 (c) Anna Schumaker
|
|
|
|
"""Helper functions for formatting strings."""
|
2022-08-10 12:22:30 -04:00
|
|
|
import re
|
|
|
|
|
|
|
|
IGNORE_WORDS = set(["a", "an", "the", ""])
|
2022-08-19 15:14:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
def search(input: str) -> str | None:
|
|
|
|
"""Translate the input string into a sqlite3 GLOB statement."""
|
|
|
|
input = input.strip().casefold()
|
|
|
|
if len(input) == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if input[0] == "^":
|
|
|
|
input = input[1:] if len(input) > 1 else "*"
|
|
|
|
elif input[0] != "*":
|
|
|
|
input = "*" + input
|
|
|
|
|
|
|
|
if input[-1] == "$":
|
|
|
|
input = input[:-1]
|
|
|
|
elif input[-1] != "*":
|
|
|
|
input += "*"
|
|
|
|
|
|
|
|
return input
|
2022-08-10 12:22:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
def sort_key(input: str) -> tuple:
|
|
|
|
"""Translate the input string into a sort key."""
|
|
|
|
if len(input) == 0:
|
|
|
|
return ()
|
|
|
|
input = re.sub(r"[\"\'’“”]", "", input.casefold())
|
|
|
|
res = re.split(r"[ /_-]", input)
|
|
|
|
if len(res) > 1 and res[0] in IGNORE_WORDS:
|
|
|
|
res = res[1:]
|
|
|
|
return tuple(res)
|