1+ from dataclasses import dataclass , field
12import random
23import re
34import string
45
56from .template_utils import TemplateRef
67
78
8- _PLACEHOLDER_PREFIX = f"t🐍{ '' .join (random .choices (string .ascii_lowercase , k = 2 ))} -"
9- _PLACEHOLDER_SUFFIX = f"-{ '' .join (random .choices (string .ascii_lowercase , k = 2 ))} 🐍t"
10- _PLACEHOLDER_PATTERN = re .compile (
11- re .escape (_PLACEHOLDER_PREFIX ) + r"(\d+)" + re .escape (_PLACEHOLDER_SUFFIX )
12- )
9+ def make_placeholder_config () -> PlaceholderConfig :
10+ prefix = f"t🐍{ '' .join (random .choices (string .ascii_lowercase , k = 2 ))} -"
11+ suffix = f"-{ '' .join (random .choices (string .ascii_lowercase , k = 2 ))} 🐍t"
12+ return PlaceholderConfig (
13+ prefix = prefix ,
14+ suffix = suffix ,
15+ pattern = re .compile (re .escape (prefix ) + r"(\d+)" + re .escape (suffix )),
16+ )
1317
1418
15- def make_placeholder ( i : int ) -> str :
16- """Generate a placeholder for the i-th interpolation."""
17- return f" { _PLACEHOLDER_PREFIX } { i } { _PLACEHOLDER_SUFFIX } "
19+ @ dataclass ( frozen = True )
20+ class PlaceholderConfig :
21+ """String operations for working with a placeholder pattern."" "
1822
23+ prefix : str
24+ suffix : str
25+ pattern : re .Pattern
1926
20- def match_placeholders ( s : str ) -> list [ re . Match [ str ]] :
21- """Find all placeholders in a string ."""
22- return list ( _PLACEHOLDER_PATTERN . finditer ( s ))
27+ def make_placeholder ( self , i : int ) -> str :
28+ """Generate a placeholder for the i-th interpolation ."""
29+ return f" { self . prefix } { i } { self . suffix } "
2330
31+ def match_placeholders (self , s : str ) -> list [re .Match [str ]]:
32+ """Find all placeholders in a string."""
33+ return list (self .pattern .finditer (s ))
2434
25- def find_placeholders (s : str ) -> TemplateRef :
26- """
27- Find all placeholders in a string and return a TemplateRef.
35+ def find_placeholders (self , s : str ) -> TemplateRef :
36+ """
37+ Find all placeholders in a string and return a TemplateRef.
2838
29- If no placeholders are found, returns a static TemplateRef.
30- """
31- matches = match_placeholders (s )
32- if not matches :
33- return TemplateRef .literal (s )
39+ If no placeholders are found, returns a static TemplateRef.
40+ """
41+ matches = self . match_placeholders (s )
42+ if not matches :
43+ return TemplateRef .literal (s )
3444
35- strings : list [str ] = []
36- i_indexes : list [int ] = []
37- last_index = 0
38- for match in matches :
39- start , end = match .span ()
40- strings .append (s [last_index :start ])
41- i_indexes .append (int (match [1 ]))
42- last_index = end
43- strings .append (s [last_index :])
45+ strings : list [str ] = []
46+ i_indexes : list [int ] = []
47+ last_index = 0
48+ for match in matches :
49+ start , end = match .span ()
50+ strings .append (s [last_index :start ])
51+ i_indexes .append (int (match [1 ]))
52+ last_index = end
53+ strings .append (s [last_index :])
4454
45- return TemplateRef (tuple (strings ), tuple (i_indexes ))
55+ return TemplateRef (tuple (strings ), tuple (i_indexes ))
4656
4757
58+ @dataclass
4859class PlaceholderState :
49- known : set [int ]
60+ known : set [int ] = field (default_factory = set )
61+ config : PlaceholderConfig = field (default_factory = make_placeholder_config )
5062 """Collection of currently 'known and active' placeholder indexes."""
5163
52- def __init__ (self ):
53- self .known = set ()
54-
5564 @property
5665 def is_empty (self ) -> bool :
5766 return len (self .known ) == 0
5867
5968 def add_placeholder (self , index : int ) -> str :
60- placeholder = make_placeholder (index )
69+ placeholder = self . config . make_placeholder (index )
6170 self .known .add (index )
6271 return placeholder
6372
@@ -69,7 +78,7 @@ def remove_placeholders(self, text: str) -> TemplateRef:
6978
7079 If no placeholders are found, returns a static PlaceholderRef.
7180 """
72- pt = find_placeholders (text )
81+ pt = self . config . find_placeholders (text )
7382 for index in pt .i_indexes :
7483 if index not in self .known :
7584 raise ValueError (f"Unknown placeholder index { index } found in text." )
0 commit comments