inst-resources/str_replace.nsh
changeset 0 67e1cc6cd929
child 3 0cce88c7cf7c
equal deleted inserted replaced
-1:000000000000 0:67e1cc6cd929
       
     1 ; StrReplace
       
     2 ; Replaces all ocurrences of a given needle within a haystack with another string
       
     3 ; Written by Dan Fuhry
       
     4 
       
     5 !ifndef _StrRep
       
     6 !define _StrRep
       
     7 
       
     8 Var sr_haystack
       
     9 Var sr_needle
       
    10 Var sr_replace
       
    11 Var sr_pos
       
    12 Var sr_needlelen
       
    13 Var sr_p_before
       
    14 Var sr_stacklen
       
    15 Var sr_p_after
       
    16 Var sr_newpos
       
    17 Var sr_test
       
    18 
       
    19 Function str_replace
       
    20   Exch $sr_replace
       
    21   Exch 1
       
    22   Exch $sr_needle
       
    23   Exch 2
       
    24   Exch $sr_haystack
       
    25     StrCpy $sr_pos -1
       
    26     StrLen $sr_needlelen $sr_needle
       
    27     StrLen $sr_stacklen $sr_haystack
       
    28     loop:
       
    29       IntOp $sr_pos $sr_pos + 1
       
    30       StrCpy $sr_test $sr_haystack $sr_needlelen $sr_pos
       
    31       StrCmp $sr_test $sr_needle found
       
    32       StrCmp $sr_pos $sr_stacklen done
       
    33       Goto loop
       
    34     found:
       
    35       StrCpy $sr_p_before $sr_haystack $sr_pos
       
    36       IntOp $sr_newpos $sr_pos + $sr_needlelen
       
    37       StrCpy $sr_p_after $sr_haystack "" $sr_newpos
       
    38       StrCpy $sr_haystack $sr_p_before$sr_replace$sr_p_after
       
    39       StrCpy $sr_pos $sr_newpos
       
    40       StrLen $sr_stacklen $sr_haystack
       
    41       Goto loop
       
    42     done:
       
    43   Pop $sr_needle ; Prevent "invalid opcode" errors and keep the
       
    44   Pop $sr_needle ; stack as it was before the function was called
       
    45   Exch $sr_haystack
       
    46 FunctionEnd
       
    47 
       
    48 !endif ; _StrRep
       
    49 
       
    50 !ifndef StrReplace
       
    51   !macro _strReplaceConstructor OUT NEEDLE NEEDLE2 HAYSTACK
       
    52     Push `${HAYSTACK}`
       
    53     Push `${NEEDLE}`
       
    54     Push `${NEEDLE2}`
       
    55     Call str_replace
       
    56     Pop `${OUT}`
       
    57   !macroend
       
    58 
       
    59   !define StrReplace '!insertmacro "_strReplaceConstructor"'
       
    60   !define str_replace '!insertmacro "_strReplaceConstructor"'
       
    61 !endif
       
    62 
       
    63 ; StrStr
       
    64 ; input, top of stack = string to search for
       
    65 ;        top of stack-1 = string to search in
       
    66 ; output, top of stack (replaces with the portion of the string remaining)
       
    67 ; modifies no other variables.
       
    68 ;
       
    69 ; Usage:
       
    70 ;   Push "this is a long ass string"
       
    71 ;   Push "ass"
       
    72 ;   Call StrStr
       
    73 ;   Pop $R0
       
    74 ;  ($R0 at this point is "ass string")
       
    75 
       
    76 !macro StrStr un
       
    77 Function ${un}StrStr
       
    78 Exch $R1 ; st=haystack,old$R1, $R1=needle
       
    79   Exch    ; st=old$R1,haystack
       
    80   Exch $R2 ; st=old$R1,old$R2, $R2=haystack
       
    81   Push $R3
       
    82   Push $R4
       
    83   Push $R5
       
    84   StrLen $R3 $R1
       
    85   StrCpy $R4 0
       
    86   ; $R1=needle
       
    87   ; $R2=haystack
       
    88   ; $R3=len(needle)
       
    89   ; $R4=cnt
       
    90   ; $R5=tmp
       
    91   loop:
       
    92     StrCpy $R5 $R2 $R3 $R4
       
    93     StrCmp $R5 $R1 done
       
    94     StrCmp $R5 "" done
       
    95     IntOp $R4 $R4 + 1
       
    96     Goto loop
       
    97 done:
       
    98   StrCpy $R1 $R2 "" $R4
       
    99   Pop $R5
       
   100   Pop $R4
       
   101   Pop $R3
       
   102   Pop $R2
       
   103   Exch $R1
       
   104 FunctionEnd
       
   105 !macroend
       
   106 
       
   107 !insertmacro StrStr ""
       
   108 ; !insertmacro StrStr "un."