Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing data within bytes...
#1
Tends to sometimes be rather hard when it shouldn't be. You may be able to note this especially well when working with bits and strings, where you have to modify characters yourself.

Or if you're a LISPer and is really annoyed by the fact that there's no general function for any sequence (Though there is, elt, but it's slower compared to other specialized functions).

So yeah, I got bored and modified some setf-functions and made a macro character which works like array-refering or hash-refering in python.

 Spoiler

Simply saying, one can refer to any sequence or array, including characters and integers by this #[]-macro:

Code:
> #[3 1] ; Second bit in the integer 3
T
> #[#\d 0] ; First bit in the character d
NIL
> (setf x #\d)
> (setf #[x 0] 1) ; Changing first bit from 0 to 1
1
> x ; x is now modified
#\e
> (setf x (make-hash-table)
        #[x 55] #("A vector" "consisting of" "strings")))
> #[x 55]
#("A vector" "consisting of" "strings")
> #[x 55 2]
"strings"
> #[x 55 2 2]
#\r
> #[x 55 2 2 1] ; Equals to (logbitp (char-int (schar (svref (gethash 55 x) 2) 2)) 1), or the second bit in #\r
T
> (setf #[x 55 2 2 1] nil)
NIL
> #[x 55 2]
"stpings"

Can't make things like this in C, python, perl or anything else, eh? Tongue1

I'm working on making it able to make dispatched arrays and such, so you can modify strings, arrays and whatever else destructively. Kinda like Python's [1:2:3] way of dealing with arrays, just through '(1 2 3) instead.

A destructive string-xoring in lisp, through one change in the #[-macro and adding in a (setf logxor)-function would be like this:
Code:
(defun string-xor (string1 string2)
  (let ((lst (coerce string2 'list)))
    (loop with circular-lst = (nconc lst lst) ; Circular list
          for i below (length string1)
          do (setf (logxor #[string1 i]) #[circular-lst i]))))

; And here's a non-destructive one:

(defun non-destructive-string-xor (string1 string2) ; Might want to shorten the name of this one
  (let ((lst (coerce string2 'list)))
    (with-output-to-string (out)
      (loop with circular-lst = (nconc lst lst)
            for i below (length string1)
            do (write-character (int-char (logxor #[string1 i] #[circular-lst i]))
                 out)))))

Which looks way prettier and easier to understand than your C or python string-xoring Stunned
 Spoiler
Reply


Messages In This Thread
Changing data within bytes... - by Nikkey - 2009-07-17, 02:29 AM
Changing data within bytes... - by Fiel - 2009-07-17, 02:43 AM
Changing data within bytes... - by Nikkey - 2009-07-17, 03:03 AM
Changing data within bytes... - by AngelSL - 2009-07-17, 04:03 AM
Changing data within bytes... - by Nikkey - 2009-07-17, 04:43 AM
Changing data within bytes... - by Spaz - 2009-07-17, 04:11 PM
Changing data within bytes... - by Dusk - 2009-07-17, 04:35 PM
Changing data within bytes... - by Kortestanov - 2009-07-17, 05:20 PM
Changing data within bytes... - by Nikkey - 2009-07-17, 06:51 PM
Changing data within bytes... - by Spaz - 2009-07-17, 07:36 PM
Changing data within bytes... - by Fiel - 2009-07-17, 07:42 PM
Changing data within bytes... - by Nikkey - 2009-07-17, 08:37 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)