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