move-strings.red 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %
  2. % MOVE-STRINGS.RED - "Fast" string copying utilities.
  3. %
  4. % Author: William F. Galway
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 8 June 1982
  9. % Copyright (c) 1982 University of Utah
  10. %
  11. % Utilities for moving subranges of strings around (and other related
  12. % operations). Written in SysLisp for speed. (Modeled after
  13. % PI:STRING-OPS.RED and PI:COPIERS.RED.)
  14. % Equivalent routines for vectors should be added (one of these days).
  15. on SysLisp;
  16. syslsp procedure MoveSubstringToFrom(DestString, SourceString,
  17. DestIndex, SourceIndex,
  18. SubrangeLength);
  19. % Quite a few arguments there, but should be clear enough? Returns the
  20. % modified destination string.
  21. % WARNING--this version screws up when destination and source overlap
  22. % (movement of one subrange of a string to another subrange of the same
  23. % string.)
  24. begin scalar rawsrc, rawdst, isrc, idst, maxindx, len, i;
  25. isrc := IntInf SourceIndex;
  26. idst := IntInf DestIndex;
  27. rawsrc := StrInf SourceString;
  28. rawdst := StrInf DestString;
  29. len := IntInf SubrangeLength;
  30. % Get upper bound on how far to copy--don't go past end of destination
  31. % or source, or subrange.
  32. % We want (i + idst) <= StrLen rawdst AND (i + isrc) <= StrLen rawsrc
  33. % AND i < SubrangeLength. (Strictly less than SubrangeLength, since i
  34. % starts at 0.) maxindx is the appropriate bound on i.
  35. maxindx := (StrLen rawdst) - idst;
  36. if maxindx >= len then
  37. maxindx := len-1;
  38. if maxindx > (StrLen rawsrc) - isrc then
  39. maxindx := (StrLen rawsrc) - isrc;
  40. i := 0;
  41. loop:
  42. % if we've run out of stuff, quit.
  43. if i > maxindx then
  44. goto loopex;
  45. % Otherwise, copy the string.
  46. StrByt(rawdst, i + idst) := StrByt(rawsrc, i + isrc);
  47. i := i+1;
  48. goto loop;
  49. loopex:
  50. return DestString;
  51. end;
  52. syslsp procedure FillSubstring(DestString, DestIndex, SubrangeLength, chr);
  53. % Fill a subrange of a string with a character code.
  54. begin scalar rawdst, rawchr, idst,len, maxindx, i;
  55. idst := IntInf DestIndex;
  56. rawdst := StrInf DestString;
  57. rawchr := IntInf chr;
  58. len := IntInf SubrangeLength;
  59. maxindx := StrLen rawdst;
  60. if maxindx >= len then
  61. maxindx := len-1;
  62. i := 0;
  63. loop:
  64. % if we've run out of stuff, quit.
  65. if i > maxindx then
  66. goto loopex;
  67. % Copy the character into the destination.
  68. StrByt(rawdst, i + idst) := rawchr;
  69. i := i+1;
  70. goto loop;
  71. loopex:
  72. return DestString;
  73. end;
  74. off SysLisp;