Skip to content

Commit

Permalink
Rename %string-field% to %sf%.
Browse files Browse the repository at this point in the history
  • Loading branch information
brown committed Feb 5, 2012
1 parent 9e326ba commit 5a4b0bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion message-test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
(repeated-cord ,(sf "225") ,(sf "325") ,(sf "525"))))

(defun field-equal (value expected)
(cond ((eq (type-of expected) 'pb::%string-field%)
(cond ((eq (type-of expected) 'pb::%sf%)
(is (string= (pb:string-value value) (pb:string-value expected))))
((vectorp value) (is (equalp value expected)))
(t (is (eql value expected)))))
Expand Down
2 changes: 1 addition & 1 deletion protoc/lisp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const char* PrimitiveTypeName(const FieldDescriptor* field) {
if (field->type() == FieldDescriptor::TYPE_BYTES) {
return "(cl:simple-array (cl:unsigned-byte 8) (cl:*))";
} else {
return "pb::%string-field%";
return "pb::%sf%";
}
case FieldDescriptor::CPPTYPE_MESSAGE:
return NULL;
Expand Down
34 changes: 14 additions & 20 deletions protocol-buffer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -109,57 +109,51 @@ beyond LIMIT, then signals PROTOCOL-BUFFER-READ-ERROR."))

;;;; Protocol buffer string fields

(cl:defclass %string-field% ()
(cl:defclass %sf% ()
((%octets% :accessor %octets%
:initarg :octets
:initform (cl:make-array 0 :element-type '(cl:unsigned-byte 8))
:type com.google.base:octet-vector
:documentation "Octet vector that holds the string field's value."))
(:documentation "A protocol buffer string field."))

(cl:defmethod cl:print-object ((string-field %string-field%) stream)
(cl:defmethod cl:print-object ((string-field %sf%) stream)
(cl:print-unreadable-object (string-field stream :type cl:t :identity cl:nil)
(cl:format stream "~S" (string-value string-field))))

(cl:declaim (cl:ftype (cl:function ((cl:or
cl:string
com.google.base:octet-vector
%string-field%))
(cl:values %string-field% cl:&optional))
(cl:declaim (cl:ftype (cl:function ((cl:or cl:string com.google.base:octet-vector %sf%))
(cl:values %sf% cl:&optional))
string-field))

(cl:defun string-field (value)
"Returns a new %STRING-FIELD% instance initialized to hold VALUE, which much
be either a Lisp string or a vector of UTF-8 encoded octets."
"Returns a new %SF% instance initialized to hold VALUE, which much be either a Lisp
string or a vector of UTF-8 encoded octets."
(cl:let ((octets
(cl:etypecase value
(cl:string (com.google.base:string-to-utf8-octets value))
(com.google.base:octet-vector value)
(%string-field% (utf8-string-value value)))))
(cl:make-instance '%string-field% :octets octets)))
(%sf% (utf8-string-value value)))))
(cl:make-instance '%sf% :octets octets)))

(cl:declaim (cl:ftype (cl:function (%string-field%) (cl:values cl:string cl:&optional))
string-value))
(cl:declaim (cl:ftype (cl:function (%sf%) (cl:values cl:string cl:&optional)) string-value))

(cl:defun string-value (string-field)
"Returns STRING-FIELD's value as a Lisp string."
(cl:declare (cl:type %string-field% string-field))
(cl:declare (cl:type %sf% string-field))
(com.google.base:utf8-octets-to-string (%octets% string-field)))

(cl:declaim (cl:ftype (cl:function (%string-field%)
(cl:values com.google.base:octet-vector cl:&optional))
(cl:declaim (cl:ftype (cl:function (%sf%) (cl:values com.google.base:octet-vector cl:&optional))
utf8-string-value))

(cl:defun utf8-string-value (string-field)
"Returns STRING-FIELD's value as a UTF-8 encoded vector of octets."
(cl:declare (cl:type %string-field% string-field))
(cl:declare (cl:type %sf% string-field))
(cl:copy-seq (cl:slot-value string-field '%octets%)))

(cl:declaim (cl:ftype (cl:function (%string-field%)
(cl:values com.google.base:vector-index cl:&optional))
(cl:declaim (cl:ftype (cl:function (%sf%) (cl:values com.google.base:vector-index cl:&optional))
%utf8-string-length%))

(cl:defun %utf8-string-length% (string-field)
"Returns the length in octets of STRING-FIELD's value."
(cl:declare (cl:type %string-field% string-field))
(cl:declare (cl:type %sf% string-field))
(cl:length (cl:slot-value string-field '%octets%)))

0 comments on commit 5a4b0bd

Please sign in to comment.