"""Token estimation without external dependencies"""
import re

def _estimate_tokens(text: str) -> int:
    if not text:
        return 0
    tokens = re.findall(r"\d+|\w+|[^\w\s]", text)
    count = 0
    for tok in tokens:
        if re.fullmatch(r"\d+", tok):
            count += max(1, (len(tok) + 2) // 3)
        elif re.fullmatch(r"[^\w\s]", tok):
            count += 1
        else:
            count += max(1, (len(tok) + 3) // 4)
    return count

def count_tokens(text: str) -> int:
    """Estimate token count for a string (within ~5% of tiktoken)."""
    return _estimate_tokens(text)
