Author Topic: Parse a string at space_chr by a specified max length  (Read 5347 times)

0 Members and 1 Guest are viewing this topic.

hermanm

  • Guest
Re: Parse a string at space_chr by a specified max length
« Reply #15 on: June 23, 2014, 01:05:29 AM »
FWIW
Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------splitstring------------------
  2. ;;;   Purpose: Split a string into shorter strings
  3. ;;;   Parameters:
  4. ;;;   in  : string to process
  5. ;;;     # : maximum length of string to return
  6. ;;;  char : character delimiter
  7. ;;;   Return: list of strings
  8. ;;;   Author : Herman Mayfarth
  9. ;;;   Date: June 2014
  10. ;;;-----------------------------------------------
  11. (defun splitstring (in char # /
  12.                     stripleft
  13.                     stripright
  14.                     chopright
  15.                     split
  16.                     out)
  17. ;;local functions
  18. ;;strip char from left side
  19. (defun stripleft (string char)
  20.   (if (> (strlen string) 0)
  21.     (if (= char (substr string 1 1))
  22.         (stripleft (setq string (substr string 2)) char)
  23.         string
  24.     );if
  25.   string ; return empty string
  26.   );if
  27. );stripleft
  28. ;;strip char from right side
  29. (defun stripright (string char)
  30.   (if (> (strlen string) 0)
  31.     (if (= char (substr string (strlen string)))
  32.         (stripright (setq string (substr string 1 (1- (strlen string)))) char)
  33.         string
  34.     );if
  35.   string ; return empty string
  36.   );if
  37. );stripright
  38. ;;strip non-chars from right side until next char is found
  39. (defun chopright (string char)
  40.   (if (> (strlen string) 0)
  41.     (if (not (= char (substr string (strlen string))))
  42.       (chopright (setq string (substr string 1 (1- (strlen string)))) char)
  43.       string
  44.     );if
  45.   string ; return empty string
  46.   );if
  47. );chopright
  48. ;;split string into chunks <= n characters
  49. ;;recursive, but creates out list as a side effect
  50. (defun split (string char n / chunk m)                                            
  51.   (setq chunk (substr string 1 n))
  52.   (if (= (strlen chunk) n);string has at least n characters
  53.   (progn
  54.     (cond
  55.         ((and (not (= (substr chunk n) char))
  56.               (= (substr string (1+ n) 1) char)) ;case I - end of word
  57.               (setq string (stripleft(substr string (1+ n)) char)
  58.                     out (cons chunk out))
  59.         )
  60.         ((and (not (= (substr chunk n) char))
  61.               (not (= (substr string (1+ n) 1) char)));case II - middle of word
  62.               (setq m
  63.                 (strlen (setq chunk (stripright (chopright chunk char) char))))
  64.               (setq string (stripleft(substr string (1+ m)) char)  
  65.                     out (cons chunk out))
  66.         )
  67.         ((= (substr chunk n) char) ;case III - delimiter
  68.             (setq string (stripleft(substr string (1+ n)) char)
  69.                   out (cons (stripright chunk char) out))
  70.         )
  71.     );cond
  72.     (split string char n)
  73.   );progn
  74.   (reverse(setq out(cons chunk out)));last chunk
  75.   );if
  76. );split
  77. ;;main program
  78.   (setq in (stripright (stripleft in char) char));strip leading & trailing chars
  79.   (split in char #)
  80. );splitstring
  81.  

test string:
Code - Auto/Visual Lisp: [Select]
  1. (setq b "   Tomorrow and tomorrow and tomorrow.  Life creeps on in its petty pace from day to day, to the last syllable of recorded time.  ")
  2.  

Test:

Command: (splitstring b sp 25)
("Tomorrow and tomorrow and" "tomorrow.  Life creeps on" "in its petty pace from" "day to day, to the last" "syllable of recorded" "time.")

I really don't like it, because out is built as a side effect, not a pure function call.

Also, it fails for this case:
Command: (splitstring b sp 5)
Hard error occurred ***
internal stack limit reached (simulated)

Oh, well.:(



David Bethel

  • Swamp Rat
  • Posts: 656
Re: Parse a string at space_chr by a specified max length
« Reply #16 on: June 23, 2014, 06:46:42 AM »

Quote
Command: (splitstring b sp 5)
Hard error occurred ***
internal stack limit reached (simulated)

This probably is the result of the words being longer than 5 characters.

It does seem to be a lot of code.  Thanks for the effort !  -David

R12 Dos - A2K