• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 18505123992

14 Oct 2025 05:30PM UTC coverage: 86.888% (-0.01%) from 86.899%
18505123992

push

github

web-flow
S3: fix `aws-global` validation in CreateBucket (#13250)

10 of 10 new or added lines in 4 files covered. (100.0%)

831 existing lines in 40 files now uncovered.

68028 of 78294 relevant lines covered (86.89%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.55
/localstack-core/localstack/utils/strings.py
1
import base64
1✔
2
import binascii
1✔
3
import hashlib
1✔
4
import itertools
1✔
5
import random
1✔
6
import re
1✔
7
import string
1✔
8
import uuid
1✔
9
import zlib
1✔
10

11
from localstack.config import DEFAULT_ENCODING
1✔
12

13
_unprintables = (
1✔
14
    range(0x00, 0x09),
15
    range(0x0A, 0x0A),
16
    range(0x0B, 0x0D),
17
    range(0x0E, 0x20),
18
    range(0xD800, 0xE000),
19
    range(0xFFFE, 0x10000),
20
)
21

22
# regular expression for unprintable characters
23
# Based on https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
24
#     #x9 | #xA | #xD | #x20 to #xD7FF | #xE000 to #xFFFD | #x10000 to #x10FFFF
25
REGEX_UNPRINTABLE_CHARS = re.compile(
1✔
26
    f"[{re.escape(''.join(map(chr, itertools.chain(*_unprintables))))}]"
27
)
28

29

30
def to_str(obj: str | bytes, encoding: str = DEFAULT_ENCODING, errors="strict") -> str:
1✔
31
    """If ``obj`` is an instance of ``binary_type``, return
32
    ``obj.decode(encoding, errors)``, otherwise return ``obj``"""
33
    return obj.decode(encoding, errors) if isinstance(obj, bytes) else obj
1✔
34

35

36
def to_bytes(obj: str | bytes, encoding: str = DEFAULT_ENCODING, errors="strict") -> bytes:
1✔
37
    """If ``obj`` is an instance of ``text_type``, return
38
    ``obj.encode(encoding, errors)``, otherwise return ``obj``"""
39
    return obj.encode(encoding, errors) if isinstance(obj, str) else obj
1✔
40

41

42
def truncate(data: str, max_length: int = 100) -> str:
1✔
43
    data = str(data or "")
1✔
44
    return (f"{data[:max_length]}...") if len(data) > max_length else data
1✔
45

46

47
def is_string(s, include_unicode=True, exclude_binary=False):
1✔
48
    if isinstance(s, bytes) and exclude_binary:
1✔
UNCOV
49
        return False
×
50
    if isinstance(s, str):
1✔
51
        return True
1✔
UNCOV
52
    if include_unicode and isinstance(s, str):
×
53
        return True
×
54
    return False
×
55

56

57
def is_string_or_bytes(s):
1✔
UNCOV
58
    return is_string(s) or isinstance(s, str) or isinstance(s, bytes)
×
59

60

61
def is_base64(s):
1✔
62
    regex = r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
1✔
63
    return is_string(s) and re.match(regex, s)
1✔
64

65

66
_re_camel_to_snake_case = re.compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")
1✔
67

68

69
def camel_to_snake_case(string: str) -> str:
1✔
70
    return _re_camel_to_snake_case.sub(r"_\1", string).replace("__", "_").lower()
1✔
71

72

73
def snake_to_camel_case(string: str, capitalize_first: bool = True) -> str:
1✔
74
    components = string.split("_")
1✔
75
    start_idx = 0 if capitalize_first else 1
1✔
76
    components = [x.title() for x in components[start_idx:]]
1✔
77
    return "".join(components)
1✔
78

79

80
def hyphen_to_snake_case(string: str) -> str:
1✔
UNCOV
81
    return string.replace("-", "_")
×
82

83

84
def canonicalize_bool_to_str(val: bool) -> str:
1✔
85
    return "true" if str(val).lower() == "true" else "false"
1✔
86

87

88
def convert_to_printable_chars(value: list | dict | str) -> str:
1✔
89
    """Removes all unprintable characters from the given string."""
90
    from localstack.utils.objects import recurse_object
1✔
91

92
    if isinstance(value, (dict, list)):
1✔
93

94
        def _convert(obj, **kwargs):
1✔
95
            if isinstance(obj, str):
1✔
96
                return convert_to_printable_chars(obj)
1✔
97
            return obj
1✔
98

99
        return recurse_object(value, _convert)
1✔
100

101
    result = REGEX_UNPRINTABLE_CHARS.sub("", value)
1✔
102
    return result
1✔
103

104

105
def first_char_to_lower(s: str) -> str:
1✔
106
    return s and f"{s[0].lower()}{s[1:]}"
1✔
107

108

109
def first_char_to_upper(s: str) -> str:
1✔
110
    return s and f"{s[0].upper()}{s[1:]}"
1✔
111

112

113
def str_to_bool(value):
1✔
114
    """Return the boolean value of the given string, or the verbatim value if it is not a string"""
115
    if isinstance(value, str):
1✔
116
        true_strings = ["true", "True"]
1✔
117
        return value in true_strings
1✔
118
    return value
1✔
119

120

121
def str_insert(string, index, content):
1✔
122
    """Insert a substring into an existing string at a certain index."""
UNCOV
123
    return f"{string[:index]}{content}{string[index:]}"
×
124

125

126
def str_remove(string, index, end_index=None):
1✔
127
    """Remove a substring from an existing string at a certain from-to index range."""
UNCOV
128
    end_index = end_index or (index + 1)
×
129
    return f"{string[:index]}{string[end_index:]}"
×
130

131

132
def str_startswith_ignore_case(value: str, prefix: str) -> bool:
1✔
UNCOV
133
    return value[: len(prefix)].lower() == prefix.lower()
×
134

135

136
def short_uid() -> str:
1✔
137
    return str(uuid.uuid4())[0:8]
1✔
138

139

140
def short_uid_from_seed(seed: str) -> str:
1✔
141
    hash = hashlib.sha1(seed.encode("utf-8")).hexdigest()
1✔
142
    truncated_hash = hash[:32]
1✔
143
    return str(uuid.UUID(truncated_hash))[0:8]
1✔
144

145

146
def long_uid() -> str:
1✔
147
    return str(uuid.uuid4())
1✔
148

149

150
def md5(string: str | bytes) -> str:
1✔
151
    m = hashlib.md5()
1✔
152
    m.update(to_bytes(string))
1✔
153
    return m.hexdigest()
1✔
154

155

156
def checksum_crc32(string: str | bytes) -> str:
1✔
157
    bytes = to_bytes(string)
1✔
158
    checksum = zlib.crc32(bytes)
1✔
159
    return base64.b64encode(checksum.to_bytes(4, "big")).decode()
1✔
160

161

162
def checksum_crc32c(string: str | bytes):
1✔
163
    # import botocore locally here to avoid a dependency of the CLI to botocore
164
    from botocore.httpchecksum import CrtCrc32cChecksum
1✔
165

166
    checksum = CrtCrc32cChecksum()
1✔
167
    checksum.update(to_bytes(string))
1✔
168
    return base64.b64encode(checksum.digest()).decode()
1✔
169

170

171
def checksum_crc64nvme(string: str | bytes):
1✔
172
    # import botocore locally here to avoid a dependency of the CLI to botocore
173
    from botocore.httpchecksum import CrtCrc64NvmeChecksum
1✔
174

175
    checksum = CrtCrc64NvmeChecksum()
1✔
176
    checksum.update(to_bytes(string))
1✔
177
    return base64.b64encode(checksum.digest()).decode()
1✔
178

179

180
def hash_sha1(string: str | bytes) -> str:
1✔
181
    digest = hashlib.sha1(to_bytes(string)).digest()
1✔
182
    return base64.b64encode(digest).decode()
1✔
183

184

185
def hash_sha256(string: str | bytes) -> str:
1✔
186
    digest = hashlib.sha256(to_bytes(string)).digest()
1✔
187
    return base64.b64encode(digest).decode()
1✔
188

189

190
def base64_to_hex(b64_string: str) -> bytes:
1✔
191
    return binascii.hexlify(base64.b64decode(b64_string))
1✔
192

193

194
def base64_decode(data: str | bytes) -> bytes:
1✔
195
    """Decode base64 data - with optional padding, and able to handle urlsafe encoding (containing -/_)."""
196
    data = to_str(data)
1✔
197
    missing_padding = len(data) % 4
1✔
198
    if missing_padding != 0:
1✔
UNCOV
199
        data = to_str(data) + "=" * (4 - missing_padding)
×
200
    if "-" in data or "_" in data:
1✔
201
        return base64.urlsafe_b64decode(data)
1✔
202
    return base64.b64decode(data)
1✔
203

204

205
def get_random_hex(length: int) -> str:
1✔
206
    return "".join(random.choices(string.hexdigits[:16], k=length)).lower()
1✔
207

208

209
def remove_leading_extra_slashes(input: str) -> str:
1✔
210
    """
211
    Remove leading extra slashes from the given input string.
212
    Example: '///foo/bar' -> '/foo/bar'
213
    """
UNCOV
214
    return re.sub(r"^/+", "/", input)
×
215

216

217
def prepend_with_slash(input: str) -> str:
1✔
218
    """
219
    Prepend a slash `/` to a given string if it does not have one already.
220
    """
221
    if not input.startswith("/"):
1✔
222
        return f"/{input}"
1✔
223
    return input
1✔
224

225

226
def key_value_pairs_to_dict(pairs: str, delimiter: str = ",", separator: str = "=") -> dict:
1✔
227
    """
228
    Converts a string of key-value pairs to a dictionary.
229

230
    Args:
231
        pairs (str): A string containing key-value pairs separated by a delimiter.
232
        delimiter (str): The delimiter used to separate key-value pairs (default is comma ',').
233
        separator (str): The separator between keys and values (default is '=').
234

235
    Returns:
236
        dict: A dictionary containing the parsed key-value pairs.
237
    """
238
    splits = [split_pair.partition(separator) for split_pair in pairs.split(delimiter)]
1✔
239
    return {key.strip(): value.strip() for key, _, value in splits}
1✔
240

241

242
def token_generator(item: str) -> str:
1✔
243
    base64_bytes = base64.b64encode(item.encode("utf-8"))
1✔
244
    token = base64_bytes.decode("utf-8")
1✔
245
    return token
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc