From 0378dda9b1b3a0793427f716516ab6acddeedc1d Mon Sep 17 00:00:00 2001 From: Sascha Matzke Date: Mon, 23 Mar 2009 08:55:40 +0100 Subject: [PATCH] added constants for byte-lengths (INT32-SIZE, CHAR-SIZE, INT64-SIZE) --- bson/writer/writer.factor | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/bson/writer/writer.factor b/bson/writer/writer.factor index 086ff2af7f..22a278e1fb 100644 --- a/bson/writer/writer.factor +++ b/bson/writer/writer.factor @@ -1,10 +1,9 @@ ! Copyright (C) 2008 Sascha Matzke. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors assocs bson.constants -byte-arrays byte-vectors calendar fry io io.binary io.encodings -io.encodings.binary io.encodings.string io.encodings.utf8 -io.streams.byte-array kernel math math.parser namespaces -quotations sequences serialize strings words ; +USING: accessors assocs bson.constants byte-arrays byte-vectors +calendar fry io io.binary io.encodings io.encodings.string +io.encodings.utf8 kernel math math.parser namespaces quotations +sequences serialize strings tools.walker words ; IN: bson.writer @@ -16,6 +15,8 @@ IN: bson.writer SYMBOL: shared-buffer CONSTANT: INT32-SIZE 4 +CONSTANT: CHAR-SIZE 1 +CONSTANT: INT64-SIZE 8 : (buffer) ( -- buffer ) shared-buffer get @@ -24,10 +25,10 @@ CONSTANT: INT32-SIZE 4 PRIVATE> : ensure-buffer ( -- ) - (buffer) drop ; + (buffer) drop ; inline : reset-buffer ( -- ) - (buffer) 0 >>length drop ; + (buffer) 0 >>length drop ; inline : with-buffer ( quot -- byte-vector ) [ (buffer) ] dip [ output-stream get ] compose @@ -67,11 +68,11 @@ M: objref bson-type? ( objref -- type ) drop T_Binary ; M: quotation bson-type? ( quotation -- type ) drop T_Binary ; M: byte-array bson-type? ( byte-array -- type ) drop T_Binary ; -: write-byte ( byte -- ) 1 >le write ; inline -: write-int32 ( int -- ) 4 >le write ; inline -: write-double ( real -- ) double>bits 8 >le write ; inline +: write-byte ( byte -- ) CHAR-SIZE >le write ; inline +: write-int32 ( int -- ) INT32-SIZE >le write ; inline +: write-double ( real -- ) double>bits INT64-SIZE >le write ; inline : write-cstring ( string -- ) utf8 encode B{ 0 } append write ; inline -: write-longlong ( object -- ) 8 >le write ; inline +: write-longlong ( object -- ) INT64-SIZE >le write ; inline : write-eoo ( -- ) T_EOO write-byte ; inline : write-type ( obj -- obj ) [ bson-type? write-byte ] keep ; inline