stricomp (from c/c++) function for autolisp

stricomp (from c/c++) function for autolisp

Moshe-A
Mentor Mentor
828 Views
6 Replies
Message 1 of 7

stricomp (from c/c++) function for autolisp

Moshe-A
Mentor
Mentor

Guys hi,

 

Did this last night, a function to compare 2 strings and ignore case but i wonder if there is a more efficient  way to do it  (maybe recursion)  any ideas will come in blessing?

 

the big numbers 6590 & 97122 return from (_case) have no meanig except for denote it is UPCASE\LOCASE

 

thanks

Moshe

 

 

; strings compare ignore case

(defun stricomp (s0 s1 / _case)

 (defun _case (n0)
  (cond
   ((and (>= n0 65) (<= n0 90))
    6590 	; upper case
   )
   ((and (>= n0 97) (<= n0 122))
    97122	; lower case
   )
  ); cond
 )_case

 (cond
  ((/= (strlen s0) (strlen s1))
   nil
  )
  ((eq s0 s1)
   t
  )
  ( t
   (vl-every
    '(lambda (n1 n2)
      (cond
       ((= (_case n1) (_case n2))
        (= n1 n2)
       )
       ((= (_case n1) 6590)
        (= (+ n1 32) n2)
       )
       ((= (_case n2) 6590) 
        (= n1 (+ n2 32))
       )
       ( t
        (= n1 n2)
       )
      ); cond
     ); lambda
    (vl-string->list s0)
    (vl-string->list s1)
   ); vl-evrey
  ); case
 ); cond
); stricomp

 

0 Likes
Accepted solutions (2)
829 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor

@Moshe-A wrote:

Did this last night, a function to compare 2 strings and ignore case but i wonder if there is a more efficient 


You mean without using strcase of course YES?

Here's the last bit

 

 

(defun stricomp_V (s1 s2)
  (vl-every '(lambda (a b)
	       (or (= a b) (= (+ a 32) b) (= a (+ 32 b)))
	     )
	    (vl-string->list s1)
	    (vl-string->list s2)
  )
)

 

_$ (STRICOMP_V "baNANa" "BAnaNA")
T
_$ (STRICOMP_V "banana" "banana")
T
_$ (STRICOMP_V "baNANa" "BAnaNA")
T
_$ (STRICOMP_V "banana" "BANANA")

 

0 Likes
Message 3 of 7

hak_vz
Advisor
Advisor

Or maybe this, if you have to compare nearly identical strings

 

 

(defun stricomp (s1 s2) (= (apply '+ (vl-string->list s1))(apply '+ (vl-string->list s2))))

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 7

Moshe-A
Mentor
Mentor

@hak_vz ,

 

very good  😀 but not for all cases

 

(stricomp_v "a#$%" "A#$%12")

T

 

(stricomp "abc" "cba")

T

 

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor
Accepted solution

@Moshe-A wrote:

(stricomp_v "a#$%" "A#$%12")


 

"as the last bit" of your code <---  @Moshe-A   

 

(defun stricomp_V (s1 s2)
  (and
    (= (strlen s1) (strlen s2))
    (vl-every '(lambda	(a b)
		  (or (= a b) (= (+ a 32) b) (= a (+ 32 b)))
		) ;_ end of lambda
	       (vl-string->list s1)
	       (vl-string->list s2)
     ) ;_ end of vl-every
  ) ;
)

 

 

 

 

 

 

0 Likes
Message 6 of 7

Moshe-A
Mentor
Mentor
Accepted solution

that's beautiful @pbejse  - thank you very much

0 Likes
Message 7 of 7

hak_vz
Advisor
Advisor

Or maybe this

 

(defun stricomp (s1 s2 case) (if (and case)  (= s1 s2)(= (strcase s1  T) (strcase s2 T))))

 

If case is important use (stricomp s1 s2  T) if not (stricomp s1 s2 nil)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes