@@ -474,11 +474,7 @@ 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 = ""
478- i = 0
479- while i < pads + 1 :
480- padding += pad_str
481- i += 1
477+ padding = pad_str * (pads // len (pad_str ) + 1 )
482478 return Functions .substr (padding , 0 , pads ) + string
483479
484480 # Source: Jsonata4Java PadFunction
@@ -501,11 +497,7 @@ def right_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str]
501497 pads = size - str_len
502498 if pads <= 0 :
503499 return string
504- padding = ""
505- i = 0
506- while i < pads + 1 :
507- padding += pad_str
508- i += 1
500+ padding = pad_str * (pads // len (pad_str ) + 1 )
509501 return string + Functions .substr (padding , 0 , pads )
510502
511503 @dataclass
@@ -767,39 +759,39 @@ def replace(string: Optional[str], pattern: Union[str, re.Pattern], replacement:
767759 raise jexception .JException ("Fourth argument of replace function must evaluate to a positive number" , 0 )
768760
769761 def string_replacer (match ):
770- result = ''
762+ parts = []
771763 position = 0
772764 repl = str (replacement )
773765 while position < len (repl ):
774766 index = repl .find ('$' , position )
775767 if index == - 1 :
776- result += repl [position :]
768+ parts . append ( repl [position :])
777769 break
778- result += repl [position :index ]
770+ parts . append ( repl [position :index ])
779771 position = index + 1
780772 if position < len (repl ):
781773 dollar_val = repl [position ]
782774 if dollar_val == '$' :
783- result += '$'
775+ parts . append ( '$' )
784776 position += 1
785777 elif dollar_val == '0' :
786- result += match .group (0 )
778+ parts . append ( match .group (0 ) )
787779 position += 1
788780 else :
789781 max_digits = len (str (len (match .groups ())))
790782 group_num = repl [position :position + max_digits ]
791783 if group_num .isdigit ():
792784 group_index = int (group_num )
793785 if 0 < group_index <= len (match .groups ()):
794- result += match .group (group_index ) or ''
786+ parts . append ( match .group (group_index ) or '' )
795787 position += len (group_num )
796788 else :
797- result += '$'
789+ parts . append ( '$' )
798790 else :
799- result += '$'
791+ parts . append ( '$' )
800792 else :
801- result += '$'
802- return result
793+ parts . append ( '$' )
794+ return '' . join ( parts )
803795
804796 if callable (replacement ):
805797 replacer = lambda m : replacement (m .groupdict ())
@@ -810,23 +802,23 @@ def string_replacer(match):
810802
811803 if isinstance (pattern , str ):
812804 # Use string methods for literal string patterns
813- result = ''
805+ parts = []
814806 position = 0
815807 count = 0
816808 while True :
817809 if limit is not None and count >= limit :
818- result += string [position :]
810+ parts . append ( string [position :])
819811 break
820812 index = string .find (pattern , position )
821813 if index == - 1 :
822- result += string [position :]
814+ parts . append ( string [position :])
823815 break
824- result += string [position :index ]
816+ parts . append ( string [position :index ])
825817 match = re .match (re .escape (pattern ), string [index :])
826- result += replacer (match )
818+ parts . append ( replacer (match ) )
827819 position = index + len (pattern )
828820 count += 1
829- return result
821+ return '' . join ( parts )
830822 else :
831823 # Use regex for pattern objects
832824 if limit is None :
0 commit comments