Skip to content

Commit

Permalink
fixed length strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ilitirit committed Aug 26, 2009
1 parent e9b13e1 commit 9910d1f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/fixed-string.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(in-package #:manardb)

(defmmclass mm-fixed-string (mm-string)
((cropped-length :type mindex :initform 0))
(walker walk-array))

(defun mm-fixed-string-uncropper (string original-length)
(declare (ignore original-length))
(concatenate 'string string "..."))

(defvar *mm-fixed-string-uncropper* 'mm-fixed-string-uncropper)

(defun mm-fixed-string-value (mfs)
(with-pointer-slots (cropped-length length)
((mm-object-pointer mfs) mm-fixed-string)
(let ((base-string (cl-irregsexp.bytestrings:force-string
(subseq
(cl-irregsexp.bytestrings:force-byte-vector
(tag-general-unbox-array (mptr-tag (%ptr mfs)) (mptr-index (%ptr mfs))))
0
(min cropped-length length)))))
(if (> cropped-length length)
(funcall *mm-fixed-string-uncropper* base-string cropped-length)
base-string))))

(with-constant-tag-for-class (element-tag boxed-byte)
(defun-speedy make-mm-fixed-string (length &key value)
(let ((mfs (make-instance 'mm-fixed-string
:length length
:base (make-mptr element-tag
(mtagmap-alloc (mtagmap element-tag)
(* length #.(stored-type-size '(unsigned-byte 8))))))))
(when value
(mm-fixed-string-store mfs value))
mfs)))

(defun mm-fixed-string-store (mfs string)
(with-pointer-slots (cropped-length length base)
((mm-object-pointer mfs) mm-fixed-string)
(let ((bv (cl-irregsexp.bytestrings:force-byte-vector string)) (ptr (mptr-pointer base)))
(setf cropped-length (length bv))
(loop for x across bv
for i below length
do (setf (d ptr i (unsigned-byte 8)) x))))
mfs)

(defun (setf mm-fixed-string-value) (string mfs)
(mm-fixed-string-store mfs string)
string)

0 comments on commit 9910d1f

Please sign in to comment.