Skip to content

Commit a119e8c

Browse files
committed
copilot feedback
1 parent 61395af commit a119e8c

1 file changed

Lines changed: 36 additions & 43 deletions

File tree

src/jsonata/functions.py

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,11 @@ def left_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str])
474474
pads = size - str_len
475475
if pads <= 0:
476476
return string
477-
padding = pad_str * (pads // len(pad_str) + 1)
477+
padding = ""
478+
i = 0
479+
while i < pads + 1:
480+
padding += pad_str
481+
i += 1
478482
return Functions.substr(padding, 0, pads) + string
479483

480484
# Source: Jsonata4Java PadFunction
@@ -497,7 +501,11 @@ def right_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str]
497501
pads = size - str_len
498502
if pads <= 0:
499503
return string
500-
padding = pad_str * (pads // len(pad_str) + 1)
504+
padding = ""
505+
i = 0
506+
while i < pads + 1:
507+
padding += pad_str
508+
i += 1
501509
return string + Functions.substr(padding, 0, pads)
502510

503511
@dataclass
@@ -549,7 +557,7 @@ def contains(string: Optional[str], token: Union[None, str, re.Pattern]) -> Opti
549557

550558
if isinstance(token, str):
551559
result = (string.find(str(token)) != - 1)
552-
elif Functions.is_regex(token):
560+
elif isinstance(token, re.Pattern):
553561
matches = Functions.evaluate_matcher(token, string)
554562
# if (dbg) System.out.println("match = "+matches)
555563
# result = (typeof matches !== 'undefined')
@@ -647,7 +655,7 @@ def safe_replace_all(s: str, pattern: re.Pattern, replacement: Optional[Any]) ->
647655
r = None
648656
for i in range(0, 10):
649657
try:
650-
r = pattern.sub(replacement, s)
658+
r = re.sub(pattern, replacement, s)
651659
break
652660
except Exception as e:
653661
msg = str(e)
@@ -704,7 +712,7 @@ def replace_fn(t):
704712
else:
705713
raise jexception.JException("D3012", -1)
706714

707-
r = pattern.sub(replace_fn, s)
715+
r = re.sub(pattern, replace_fn, s)
708716
return r
709717

710718
#
@@ -721,7 +729,7 @@ def safe_replace_first(s: str, pattern: re.Pattern, replacement: str) -> Optiona
721729
r = None
722730
for i in range(0, 10):
723731
try:
724-
r = pattern.sub(replacement, s, count=1)
732+
r = re.sub(pattern, replacement, s, count=1)
725733
break
726734
except Exception as e:
727735
msg = str(e)
@@ -759,39 +767,39 @@ def replace(string: Optional[str], pattern: Union[str, re.Pattern], replacement:
759767
raise jexception.JException("Fourth argument of replace function must evaluate to a positive number", 0)
760768

761769
def string_replacer(match):
762-
parts = []
770+
result = ''
763771
position = 0
764772
repl = str(replacement)
765773
while position < len(repl):
766774
index = repl.find('$', position)
767775
if index == -1:
768-
parts.append(repl[position:])
776+
result += repl[position:]
769777
break
770-
parts.append(repl[position:index])
778+
result += repl[position:index]
771779
position = index + 1
772780
if position < len(repl):
773781
dollar_val = repl[position]
774782
if dollar_val == '$':
775-
parts.append('$')
783+
result += '$'
776784
position += 1
777785
elif dollar_val == '0':
778-
parts.append(match.group(0))
786+
result += match.group(0)
779787
position += 1
780788
else:
781789
max_digits = len(str(len(match.groups())))
782790
group_num = repl[position:position+max_digits]
783791
if group_num.isdigit():
784792
group_index = int(group_num)
785793
if 0 < group_index <= len(match.groups()):
786-
parts.append(match.group(group_index) or '')
794+
result += match.group(group_index) or ''
787795
position += len(group_num)
788796
else:
789-
parts.append('$')
797+
result += '$'
790798
else:
791-
parts.append('$')
799+
result += '$'
792800
else:
793-
parts.append('$')
794-
return ''.join(parts)
801+
result += '$'
802+
return result
795803

796804
if callable(replacement):
797805
replacer = lambda m: replacement(m.groupdict())
@@ -802,23 +810,23 @@ def string_replacer(match):
802810

803811
if isinstance(pattern, str):
804812
# Use string methods for literal string patterns
805-
parts = []
813+
result = ''
806814
position = 0
807815
count = 0
808816
while True:
809817
if limit is not None and count >= limit:
810-
parts.append(string[position:])
818+
result += string[position:]
811819
break
812820
index = string.find(pattern, position)
813821
if index == -1:
814-
parts.append(string[position:])
822+
result += string[position:]
815823
break
816-
parts.append(string[position:index])
824+
result += string[position:index]
817825
match = re.match(re.escape(pattern), string[index:])
818-
parts.append(replacer(match))
826+
result += replacer(match)
819827
position = index + len(pattern)
820828
count += 1
821-
return ''.join(parts)
829+
return result
822830
else:
823831
# Use regex for pattern objects
824832
if limit is None:
@@ -1836,8 +1844,7 @@ def each(obj: Optional[Mapping], func: Any) -> Optional[list]:
18361844
#
18371845
@staticmethod
18381846
def error(message: Optional[str]) -> NoReturn:
1839-
raise jexception.JException("D3137", -1,
1840-
message if message is not None else "$error() function evaluated")
1847+
raise jexception.JException("D3137", -1, message if message is not None else "$error() function evaluated")
18411848

18421849
#
18431850
#
@@ -1852,8 +1859,8 @@ def assert_fn(condition: Optional[bool], message: Optional[str]) -> None:
18521859
raise jexception.JException("T0410", -1)
18531860

18541861
if not condition:
1855-
raise jexception.JException("D3141", -1,
1856-
message if message is not None else "$assert() statement failed")
1862+
raise jexception.JException("D3141", -1, "$assert() statement failed")
1863+
# message: message || "$assert() statement failed"
18571864

18581865
#
18591866
#
@@ -2019,22 +2026,6 @@ def append(arg1: Optional[Any], arg2: Optional[Any]) -> Optional[Any]:
20192026
def is_lambda(result: Optional[Any]) -> bool:
20202027
return isinstance(result, parser.Parser.Symbol) and result._jsonata_lambda
20212028

2022-
#
2023-
# Tests whether a value is a compiled regex, from the stdlib re module
2024-
# or from a pluggable regex_engine (e.g. re2) with a compatible interface.
2025-
#
2026-
@staticmethod
2027-
def is_regex(value: Optional[Any]) -> bool:
2028-
if isinstance(value, re.Pattern):
2029-
return True
2030-
if value is None or inspect.ismodule(value) or inspect.isclass(value):
2031-
return False
2032-
return (
2033-
callable(getattr(value, "search", None))
2034-
and callable(getattr(value, "finditer", None))
2035-
and callable(getattr(value, "sub", None))
2036-
)
2037-
20382029
#
20392030
# Return value from an object for a given key
20402031
# @param {Object} input - Object/Array
@@ -2116,6 +2107,8 @@ def datetime_to_millis(timestamp: Optional[str], picture: Optional[str]) -> Opti
21162107
dt = datetime.datetime.fromisoformat(timestamp)
21172108
if dt.tzinfo is None:
21182109
dt = dt.replace(tzinfo=datetime.timezone.utc)
2110+
else:
2111+
dt = dt.astimezone(datetime.timezone.utc)
21192112
return int(dt.timestamp() * 1000)
21202113
# try:
21212114
# size = len(timestamp)
@@ -2217,7 +2210,7 @@ def function_eval(expr: Optional[str], focus: Optional[Any]) -> Optional[Any]:
22172210

22182211
ast = None
22192212
try:
2220-
ast = jsonata.Jsonata(expr, jsonata.Jsonata.CURRENT.jsonata.regex_engine)
2213+
ast = jsonata.Jsonata(expr)
22212214
except Exception as err:
22222215
# error parsing the expression passed to $eval
22232216
# populateMessage(err)

0 commit comments

Comments
 (0)