init
This commit is contained in:
82
arcaea_apk_assets/parsers/_shared.py
Normal file
82
arcaea_apk_assets/parsers/_shared.py
Normal file
@ -0,0 +1,82 @@
|
||||
"""
|
||||
Copyright (C) 2025 283375
|
||||
|
||||
This file is part of "arcaea-apk-assets" (stated as "this program" below).
|
||||
|
||||
This program is free software: you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation, either version 3 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def is_string_blank(string: str):
|
||||
"""
|
||||
Check if a string equals to a blank string after stripped.
|
||||
|
||||
>>> is_string_blank("abc")
|
||||
False
|
||||
|
||||
>>> is_string_blank("")
|
||||
True
|
||||
|
||||
>>> is_string_blank(" ")
|
||||
True
|
||||
|
||||
>>> is_string_blank(" a ")
|
||||
False
|
||||
"""
|
||||
|
||||
if not isinstance(string, str):
|
||||
msg = "A `str` object is expected"
|
||||
raise TypeError(msg)
|
||||
|
||||
return string.strip() == ""
|
||||
|
||||
|
||||
def blank_str_to_none(string: str):
|
||||
"""
|
||||
Convert a blank string (see :func:`~is_string_blank`) to None, otherwise return the
|
||||
original string.
|
||||
|
||||
>>> blank_str_to_none("") is None
|
||||
True
|
||||
|
||||
>>> blank_str_to_none("abc") == "abc"
|
||||
True
|
||||
|
||||
>>> blank_str_to_none(" ") is None
|
||||
True
|
||||
|
||||
>>> blank_str_to_none(" abc ") == " abc "
|
||||
True
|
||||
|
||||
>>> blank_str_to_none(None) is None
|
||||
True
|
||||
"""
|
||||
|
||||
if string is None:
|
||||
return None
|
||||
|
||||
if not isinstance(string, str):
|
||||
msg = "A `str` object is expected"
|
||||
raise TypeError(msg)
|
||||
|
||||
return None if is_string_blank(string) else string
|
||||
|
||||
|
||||
def replace_single_newline(text: Optional[str]):
|
||||
if text is None:
|
||||
return None
|
||||
|
||||
return re.sub(r"\n+", lambda m: "\n" * (len(m.group()) - 1), text)
|
Reference in New Issue
Block a user