refactor(state): CJK range table; shared _coerce_or for import numeric coercion

This commit is contained in:
Teknium
2026-09-02 16:29:12 -07:00
parent c5777cfa22
commit d86ebe66c0
2 changed files with 20 additions and 20 deletions

View File

@@ -350,13 +350,22 @@ class SessionPortabilityMixin:
raise ValueError(f"{field} must be JSON serializable") from exc
@staticmethod
def _float_or_none(value: Any) -> Optional[float]:
def _coerce_or(value: Any, cast, default):
"""``cast(value)``; *default* for None or an unparsable value."""
if value is None:
return None
return default
try:
return float(value)
return cast(value)
except (TypeError, ValueError):
return None
return default
@classmethod
def _float_or_none(cls, value: Any) -> Optional[float]:
return cls._coerce_or(value, float, None)
@classmethod
def _int_or_default(cls, value: Any, default: int = 0) -> int:
return cls._coerce_or(value, int, default)
@staticmethod
def _import_int_or_none(value: Any, field: str) -> Optional[int]:
@@ -367,15 +376,6 @@ class SessionPortabilityMixin:
except (TypeError, ValueError) as exc:
raise ValueError(f"{field} must be an integer") from exc
@staticmethod
def _int_or_default(value: Any, default: int = 0) -> int:
if value is None:
return default
try:
return int(value)
except (TypeError, ValueError):
return default
@staticmethod
def _reasoning_json_value(value: Any) -> Any:
if not isinstance(value, str):

View File

@@ -824,15 +824,15 @@ class SessionSearchMixin:
sanitized = sanitized.replace(f"\x00Q{i}\x00", quoted)
return sanitized.strip()
# Unified Ideographs, Extension A, Extension B, CJK Symbols, Hiragana, Katakana, Hangul Syllables.
_CJK_RANGES = (
(0x4E00, 0x9FFF), (0x3400, 0x4DBF), (0x20000, 0x2A6DF), (0x3000, 0x303F), (0x3040, 0x309F),
(0x30A0, 0x30FF), (0xAC00, 0xD7AF),
)
@staticmethod
def _is_cjk_codepoint(cp: int) -> bool:
return (0x4E00 <= cp <= 0x9FFF or # CJK Unified Ideographs
0x3400 <= cp <= 0x4DBF or # CJK Extension A
0x20000 <= cp <= 0x2A6DF or # CJK Extension B
0x3000 <= cp <= 0x303F or # CJK Symbols
0x3040 <= cp <= 0x309F or # Hiragana
0x30A0 <= cp <= 0x30FF or # Katakana
0xAC00 <= cp <= 0xD7AF) # Hangul Syllables
return any(lo <= cp <= hi for lo, hi in SessionSearchMixin._CJK_RANGES)
@staticmethod
def _contains_cjk(text: str) -> bool: