Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for far.rex

(view source code of far.rex as plain text)

  1. /* FaR, Find and Replace */
  2.  
  3. /* Check command line arguments */
  4. Parse Arg string0 string1 string2 caseoff dummy
  5. If string1 = "" Then Call Syntax
  6. If dummy  <> "" Then Call Syntax
  7. If caseoff = "" Then Do
  8. 	case = 1
  9. End
  10. Else Do
  11. 	If Translate( caseoff ) = "-I" Then Do
  12. 		case     = 0
  13. 		ustring0 = Translate( string0 )
  14. 		ustring1 = Translate( string1 )
  15. 	End
  16. 	Else Do
  17. 		Call Syntax
  18. 	End
  19. End
  20. len1  = Length( string1 )
  21. len2  = Length( string2 )
  22.  
  23. /* Search and replace */
  24. found = 1
  25. start = 1
  26. Do Forever
  27. 	If case = 1 Then Do
  28. 		found = Pos( string1, string0, start )
  29. 	End
  30. 	Else Do
  31. 		found = Pos( ustring1, ustring0, start )
  32. 	End
  33. 	If found = 0 Then Leave
  34. 	string0  = SubStr( string0, 1, found - 1 )||string2||SubStr( string0, found + len1 )
  35. 	start    = Max( 1, start + len2 )
  36. 	If case  = 0 Then ustring0 = Translate( string0 )
  37. End
  38.  
  39. /* Display the end result */
  40. Say string0
  41.  
  42. /* Normal program termination */
  43. Exit 0
  44.  
  45. Syntax:
  46. 	Say
  47. 	Say "FaR -- Find and Replace,  Version 1.01"
  48. 	Say "Search a string for a substring and replace it with another substring"
  49. 	Say
  50. 	Say 'Usage:  FAR.REX  "string0"  "string1"  [ "string2"  [ -I ] ]'
  51. 	Say
  52. 	Say 'Where:  "string0" is the string to be searched'
  53. 	Say '        "string1" is the substring to search for'
  54. 	Say '        "string2" is the substring to replace string1 (default: empty)'
  55. 	Say "        -I        makes the search case insensitive   (requires string2)"
  56. 	Say
  57. 	Say "Written by Rob van der Woude"
  58. 	Say "http://www.robvanderwoude.com"
  59. 	Exit 1
  60. Return
  61.  

page last modified: 2024-02-26; loaded in 0.0174 seconds