parsestring.cfg 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const breakparse [ parsebreak = 1 ]
  2. const doparse [
  3. tempalias parselen (strlen $strtoparse)
  4. loop dpl $parselen [
  5. if $parsebreak break [
  6. if $parsebackwards [
  7. $charcontainer = (substr $strtoparse (- (- $parselen $dpl) 1) 1)
  8. (format __%1 $charcontainer) = (- (- $parselen $dpl) 1)
  9. ] [
  10. $charcontainer = (substr $strtoparse $dpl 1)
  11. (format __%1 $charcontainer) = $dpl
  12. ]
  13. execute $parsecode
  14. ]
  15. ]
  16. ]
  17. const parsestring [
  18. if (> $numargs 2) [
  19. tempalias strtoparse $arg1
  20. tempalias charcontainer $arg2
  21. tempalias parsecode $arg3
  22. if (> $numargs 3) [
  23. tempalias parsebackwards $arg4
  24. ] [ tempalias parsebackwards 0 ]
  25. tempalias parsebreak 0
  26. doparse
  27. ]
  28. ]
  29. // make helpful consts for use with (testchar)
  30. const CHARACTERTESTS [ISDIGIT ISALPHA ISALNUM ISLOWERCASE ISUPPERCASE ISCHAR ISPUNCT ISWHITESPACE]
  31. loop cel (listlen $CHARACTERTESTS) [
  32. const (at $CHARACTERTESTS $cel) $cel
  33. ]
  34. // trimAllUnnecessaryWhitespace - Returns a modified string after removing any unnecessary leading and trailing whitespace from the original.
  35. const trimAllUnnecessaryWhitespace [
  36. tempalias strtotrim $arg1
  37. forceinit [charsbegin charsend] -1
  38. parsestring $strtotrim tch [
  39. if (! (testchar $tch $ISWHITESPACE)) [
  40. charsbegin = $__tch
  41. breakparse
  42. ]
  43. ]
  44. parsestring $strtotrim thc [
  45. if (! (testchar $thc $ISWHITESPACE)) [
  46. charsend = $__thc
  47. breakparse
  48. ]
  49. ] 1
  50. if (&& (&& (!= $charsbegin -1) (!= $charsend -1)) (!= $charsbegin $charsend)) [
  51. result (substr $strtotrim $charsbegin (- (+ $charsend 1) $charsbegin))
  52. ] [ result $strtotrim ]
  53. ]
  54. // trimAllWhitespace - Returns a modified string after removing any whitespace characters from the original.
  55. const trimAllWhitespace [
  56. tempalias strtotrim $arg1
  57. trimmedstr = []
  58. parsestring $strtotrim twh [
  59. if (! (testchar $twh $ISWHITESPACE)) [
  60. trimmedstr = (concatword $trimmedstr $twh)
  61. ]
  62. ]
  63. result $trimmedstr
  64. ]