stringx.sl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % STRINGX - Useful String Functions
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 9 September 1982
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. (CompileTime (load fast-int fast-strings common))
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12. % Private Macros:
  13. (CompileTime (progn
  14. (put 'make-string 'cmacro % temporary bug fix
  15. '(lambda (sz init)
  16. (mkstring (- sz 1) init)))
  17. )) % End of CompileTime
  18. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19. (de string-rest (s i)
  20. (substring s i (string-length s)))
  21. (de string-pad-right (s desired-length)
  22. % Pad the specified string with spaces on the right side to the specified
  23. % length. Returns a new string.
  24. (let ((len (string-length s)))
  25. (if (< len desired-length)
  26. (string-concat s (make-string (- desired-length len) #\space))
  27. s)))
  28. (de string-pad-left (s desired-length)
  29. % Pad the specified string with spaces on the left side to the specified
  30. % length. Returns a new string.
  31. (let ((len (string-length s)))
  32. (if (< len desired-length)
  33. (string-concat (make-string (- desired-length len) #\space) s)
  34. s)))
  35. (de string-largest-common-prefix (s1 s2)
  36. % Return the string that is the largest common prefix of S1 and S2.
  37. (for (from i 0 (min (string-upper-bound s1) (string-upper-bound s2)) 1)
  38. (while (= (string-fetch s1 i) (string-fetch s2 i)))
  39. (returns (substring s1 0 i))
  40. ))
  41. (de strings-largest-common-prefix (l)
  42. % Return the string that is the largest common prefix of the elements
  43. % of L, which must be a list of strings.
  44. (cond ((null l) "")
  45. ((null (cdr l)) (car l))
  46. (t
  47. (let* ((prefix (car l))
  48. (limit (string-length prefix))
  49. )
  50. % Prefix[0..LIMIT-1] is the string that is a prefix of all
  51. % strings so far examined.
  52. (for (in s (cdr l))
  53. (with i)
  54. (do (let ((n (string-length s)))
  55. (if (< n limit) (setf limit n))
  56. )
  57. (setf i 0)
  58. (while (< i limit)
  59. (if (~= (string-fetch prefix i) (string-fetch s i))
  60. (setf limit i)
  61. (setf i (+ i 1))
  62. ))
  63. ))
  64. (substring prefix 0 limit)
  65. ))))