-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_placeholders.py
36 lines (32 loc) · 1.64 KB
/
find_placeholders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import re
# According to printf format string specification from Wikipedia, the format is:
# %[parameter][flags][width][.precision][length]type
# see http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders
PLACEHOLDERS_REGEX = """
( # Capture everything in group 1
% # Look for a literal '%'
(?: # Match an actual placeholder (and not '%%')
(?:\d+\$)? # Look for an optional paramter - number and then '$'
[-+ 0#]{0,5} # 0 to 5 optional flags
(?:\d+|\*)? # Optional width
(?:\.(?:\d+|\*))? # Optional precision
(?:hh|h|l|ll|L|z|j|t|q|I|I32|I64|w)? # Optional length
[diufFeEgGxXoscqaAn] # Type
| # OR
%)) # literal "%%"
"""
def find_placeholders_in_format_string(format_string):
"""
Find inside a C-style format string, all the the placeholders for parameters
i.e. for "Worker name is %s and id is %d" will return the indexes and the placeholders:
[(15, "%s"), (28, "%d")]
:param format_string: The string to find placeholders in
:return: List of placeholders location and value
"""
matches = re.finditer(PLACEHOLDERS_REGEX, format_string, flags=re.X)
results = []
for i in matches:
# Remove all the "%%" found. These are literal "%", not placeholders
if i.group(0) != "%%":
results.append((i.start(0), i.group(0)))
return results