@@ -162,14 +162,20 @@ void janet_buffer_push_u64(JanetBuffer *buffer, uint64_t x) {
162162
163163/* C functions */
164164
165- static Janet cfun_buffer_new (int32_t argc , Janet * argv ) {
165+ JANET_CORE_FN (cfun_buffer_new ,
166+ "(buffer/new capacity)" ,
167+ "Creates a new, empty buffer with enough backing memory for capacity bytes. "
168+ "Returns a new buffer of length 0." ) {
166169 janet_fixarity (argc , 1 );
167170 int32_t cap = janet_getinteger (argv , 0 );
168171 JanetBuffer * buffer = janet_buffer (cap );
169172 return janet_wrap_buffer (buffer );
170173}
171174
172- static Janet cfun_buffer_new_filled (int32_t argc , Janet * argv ) {
175+ JANET_CORE_FN (cfun_buffer_new_filled ,
176+ "(buffer/new-filled count &opt byte)" ,
177+ "Creates a new buffer of length count filled with byte. By default, byte is 0. "
178+ "Returns the new buffer." ) {
173179 janet_arity (argc , 1 , 2 );
174180 int32_t count = janet_getinteger (argv , 0 );
175181 int32_t byte = 0 ;
@@ -183,7 +189,10 @@ static Janet cfun_buffer_new_filled(int32_t argc, Janet *argv) {
183189 return janet_wrap_buffer (buffer );
184190}
185191
186- static Janet cfun_buffer_fill (int32_t argc , Janet * argv ) {
192+ JANET_CORE_FN (cfun_buffer_fill ,
193+ "(buffer/fill buffer &opt byte)" ,
194+ "Fill up a buffer with bytes, defaulting to 0s. Does not change the buffer's length. "
195+ "Returns the modified buffer." ) {
187196 janet_arity (argc , 1 , 2 );
188197 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
189198 int32_t byte = 0 ;
@@ -196,7 +205,10 @@ static Janet cfun_buffer_fill(int32_t argc, Janet *argv) {
196205 return argv [0 ];
197206}
198207
199- static Janet cfun_buffer_trim (int32_t argc , Janet * argv ) {
208+ JANET_CORE_FN (cfun_buffer_trim ,
209+ "(buffer/trim buffer)" ,
210+ "Set the backing capacity of the buffer to the current length of the buffer. Returns the "
211+ "modified buffer." ) {
200212 janet_fixarity (argc , 1 );
201213 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
202214 if (buffer -> count < buffer -> capacity ) {
@@ -211,7 +223,10 @@ static Janet cfun_buffer_trim(int32_t argc, Janet *argv) {
211223 return argv [0 ];
212224}
213225
214- static Janet cfun_buffer_u8 (int32_t argc , Janet * argv ) {
226+ JANET_CORE_FN (cfun_buffer_u8 ,
227+ "(buffer/push-byte buffer & xs)" ,
228+ "Append bytes to a buffer. Will expand the buffer as necessary. "
229+ "Returns the modified buffer. Will throw an error if the buffer overflows." ) {
215230 int32_t i ;
216231 janet_arity (argc , 1 , -1 );
217232 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
@@ -221,7 +236,11 @@ static Janet cfun_buffer_u8(int32_t argc, Janet *argv) {
221236 return argv [0 ];
222237}
223238
224- static Janet cfun_buffer_word (int32_t argc , Janet * argv ) {
239+ JANET_CORE_FN (cfun_buffer_word ,
240+ "(buffer/push-word buffer & xs)" ,
241+ "Append machine words to a buffer. The 4 bytes of the integer are appended "
242+ "in twos complement, little endian order, unsigned for all x. Returns the modified buffer. Will "
243+ "throw an error if the buffer overflows." ) {
225244 int32_t i ;
226245 janet_arity (argc , 1 , -1 );
227246 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
@@ -235,7 +254,12 @@ static Janet cfun_buffer_word(int32_t argc, Janet *argv) {
235254 return argv [0 ];
236255}
237256
238- static Janet cfun_buffer_chars (int32_t argc , Janet * argv ) {
257+ JANET_CORE_FN (cfun_buffer_chars ,
258+ "(buffer/push-string buffer & xs)" ,
259+ "Push byte sequences onto the end of a buffer. "
260+ "Will accept any of strings, keywords, symbols, and buffers. "
261+ "Returns the modified buffer. "
262+ "Will throw an error if the buffer overflows." ) {
239263 int32_t i ;
240264 janet_arity (argc , 1 , -1 );
241265 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
@@ -250,7 +274,13 @@ static Janet cfun_buffer_chars(int32_t argc, Janet *argv) {
250274 return argv [0 ];
251275}
252276
253- static Janet cfun_buffer_push (int32_t argc , Janet * argv ) {
277+ JANET_CORE_FN (cfun_buffer_push ,
278+ "(buffer/push buffer & xs)" ,
279+ "Push both individual bytes and byte sequences to a buffer. For each x in xs, "
280+ "push the byte if x is an integer, otherwise push the bytesequence to the buffer. "
281+ "Thus, this function behaves like both `buffer/push-string` and `buffer/push-byte`. "
282+ "Returns the modified buffer. "
283+ "Will throw an error if the buffer overflows." ) {
254284 int32_t i ;
255285 janet_arity (argc , 1 , -1 );
256286 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
@@ -270,14 +300,19 @@ static Janet cfun_buffer_push(int32_t argc, Janet *argv) {
270300}
271301
272302
273- static Janet cfun_buffer_clear (int32_t argc , Janet * argv ) {
303+ JANET_CORE_FN (cfun_buffer_clear ,
304+ "(buffer/clear buffer)" ,
305+ "Sets the size of a buffer to 0 and empties it. The buffer retains "
306+ "its memory so it can be efficiently refilled. Returns the modified buffer." ) {
274307 janet_fixarity (argc , 1 );
275308 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
276309 buffer -> count = 0 ;
277310 return argv [0 ];
278311}
279312
280- static Janet cfun_buffer_popn (int32_t argc , Janet * argv ) {
313+ JANET_CORE_FN (cfun_buffer_popn ,
314+ "(buffer/popn buffer n)" ,
315+ "Removes the last n bytes from the buffer. Returns the modified buffer." ) {
281316 janet_fixarity (argc , 2 );
282317 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
283318 int32_t n = janet_getinteger (argv , 1 );
@@ -290,7 +325,12 @@ static Janet cfun_buffer_popn(int32_t argc, Janet *argv) {
290325 return argv [0 ];
291326}
292327
293- static Janet cfun_buffer_slice (int32_t argc , Janet * argv ) {
328+ JANET_CORE_FN (cfun_buffer_slice ,
329+ "(buffer/slice bytes &opt start end)" ,
330+ "Takes a slice of a byte sequence from start to end. The range is half open, "
331+ "[start, end). Indexes can also be negative, indicating indexing from the end of the "
332+ "end of the array. By default, start is 0 and end is the length of the buffer. "
333+ "Returns a new buffer." ) {
294334 JanetByteView view = janet_getbytes (argv , 0 );
295335 JanetRange range = janet_getslice (argc , argv );
296336 JanetBuffer * buffer = janet_buffer (range .end - range .start );
@@ -314,7 +354,9 @@ static void bitloc(int32_t argc, Janet *argv, JanetBuffer **b, int32_t *index, i
314354 * bit = which_bit ;
315355}
316356
317- static Janet cfun_buffer_bitset (int32_t argc , Janet * argv ) {
357+ JANET_CORE_FN (cfun_buffer_bitset ,
358+ "(buffer/bit-set buffer index)" ,
359+ "Sets the bit at the given bit-index. Returns the buffer." ) {
318360 int bit ;
319361 int32_t index ;
320362 JanetBuffer * buffer ;
@@ -323,7 +365,9 @@ static Janet cfun_buffer_bitset(int32_t argc, Janet *argv) {
323365 return argv [0 ];
324366}
325367
326- static Janet cfun_buffer_bitclear (int32_t argc , Janet * argv ) {
368+ JANET_CORE_FN (cfun_buffer_bitclear ,
369+ "(buffer/bit-clear buffer index)" ,
370+ "Clears the bit at the given bit-index. Returns the buffer." ) {
327371 int bit ;
328372 int32_t index ;
329373 JanetBuffer * buffer ;
@@ -332,15 +376,19 @@ static Janet cfun_buffer_bitclear(int32_t argc, Janet *argv) {
332376 return argv [0 ];
333377}
334378
335- static Janet cfun_buffer_bitget (int32_t argc , Janet * argv ) {
379+ JANET_CORE_FN (cfun_buffer_bitget ,
380+ "(buffer/bit buffer index)" ,
381+ "Gets the bit at the given bit-index. Returns true if the bit is set, false if not." ) {
336382 int bit ;
337383 int32_t index ;
338384 JanetBuffer * buffer ;
339385 bitloc (argc , argv , & buffer , & index , & bit );
340386 return janet_wrap_boolean (buffer -> data [index ] & (1 << bit ));
341387}
342388
343- static Janet cfun_buffer_bittoggle (int32_t argc , Janet * argv ) {
389+ JANET_CORE_FN (cfun_buffer_bittoggle ,
390+ "(buffer/bit-toggle buffer index)" ,
391+ "Toggles the bit at the given bit index in buffer. Returns the buffer." ) {
344392 int bit ;
345393 int32_t index ;
346394 JanetBuffer * buffer ;
@@ -349,7 +397,11 @@ static Janet cfun_buffer_bittoggle(int32_t argc, Janet *argv) {
349397 return argv [0 ];
350398}
351399
352- static Janet cfun_buffer_blit (int32_t argc , Janet * argv ) {
400+ JANET_CORE_FN (cfun_buffer_blit ,
401+ "(buffer/blit dest src &opt dest-start src-start src-end)" ,
402+ "Insert the contents of src into dest. Can optionally take indices that "
403+ "indicate which part of src to copy into which part of dest. Indices can be "
404+ "negative to index from the end of src or dest. Returns dest." ) {
353405 janet_arity (argc , 2 , 5 );
354406 JanetBuffer * dest = janet_getbuffer (argv , 0 );
355407 JanetByteView src = janet_getbytes (argv , 1 );
@@ -386,124 +438,37 @@ static Janet cfun_buffer_blit(int32_t argc, Janet *argv) {
386438 return argv [0 ];
387439}
388440
389- static Janet cfun_buffer_format (int32_t argc , Janet * argv ) {
441+ JANET_CORE_FN (cfun_buffer_format ,
442+ "(buffer/format buffer format & args)" ,
443+ "Snprintf like functionality for printing values into a buffer. Returns "
444+ " the modified buffer." ) {
390445 janet_arity (argc , 2 , -1 );
391446 JanetBuffer * buffer = janet_getbuffer (argv , 0 );
392447 const char * strfrmt = (const char * ) janet_getstring (argv , 1 );
393448 janet_buffer_format (buffer , strfrmt , 1 , argc , argv );
394449 return argv [0 ];
395450}
396451
397- static const JanetReg buffer_cfuns [] = {
398- {
399- "buffer/new" , cfun_buffer_new ,
400- JDOC ("(buffer/new capacity)\n\n"
401- "Creates a new, empty buffer with enough backing memory for capacity bytes. "
402- "Returns a new buffer of length 0." )
403- },
404- {
405- "buffer/new-filled" , cfun_buffer_new_filled ,
406- JDOC ("(buffer/new-filled count &opt byte)\n\n"
407- "Creates a new buffer of length count filled with byte. By default, byte is 0. "
408- "Returns the new buffer." )
409- },
410- {
411- "buffer/fill" , cfun_buffer_fill ,
412- JDOC ("(buffer/fill buffer &opt byte)\n\n"
413- "Fill up a buffer with bytes, defaulting to 0s. Does not change the buffer's length. "
414- "Returns the modified buffer." )
415- },
416- {
417- "buffer/trim" , cfun_buffer_trim ,
418- JDOC ("(buffer/trim buffer)\n\n"
419- "Set the backing capacity of the buffer to the current length of the buffer. Returns the "
420- "modified buffer." )
421- },
422- {
423- "buffer/push-byte" , cfun_buffer_u8 ,
424- JDOC ("(buffer/push-byte buffer & xs)\n\n"
425- "Append bytes to a buffer. Will expand the buffer as necessary. "
426- "Returns the modified buffer. Will throw an error if the buffer overflows." )
427- },
428- {
429- "buffer/push-word" , cfun_buffer_word ,
430- JDOC ("(buffer/push-word buffer & xs)\n\n"
431- "Append machine words to a buffer. The 4 bytes of the integer are appended "
432- "in twos complement, little endian order, unsigned for all x. Returns the modified buffer. Will "
433- "throw an error if the buffer overflows." )
434- },
435- {
436- "buffer/push-string" , cfun_buffer_chars ,
437- JDOC ("(buffer/push-string buffer & xs)\n\n"
438- "Push byte sequences onto the end of a buffer. "
439- "Will accept any of strings, keywords, symbols, and buffers. "
440- "Returns the modified buffer. "
441- "Will throw an error if the buffer overflows." )
442- },
443- {
444- "buffer/push" , cfun_buffer_push ,
445- JDOC ("(buffer/push buffer & xs)\n\n"
446- "Push both individual bytes and byte sequences to a buffer. For each x in xs, "
447- "push the byte if x is an integer, otherwise push the bytesequence to the buffer. "
448- "Thus, this function behaves like both `buffer/push-string` and `buffer/push-byte`. "
449- "Returns the modified buffer. "
450- "Will throw an error if the buffer overflows." )
451- },
452- {
453- "buffer/popn" , cfun_buffer_popn ,
454- JDOC ("(buffer/popn buffer n)\n\n"
455- "Removes the last n bytes from the buffer. Returns the modified buffer." )
456- },
457- {
458- "buffer/clear" , cfun_buffer_clear ,
459- JDOC ("(buffer/clear buffer)\n\n"
460- "Sets the size of a buffer to 0 and empties it. The buffer retains "
461- "its memory so it can be efficiently refilled. Returns the modified buffer." )
462- },
463- {
464- "buffer/slice" , cfun_buffer_slice ,
465- JDOC ("(buffer/slice bytes &opt start end)\n\n"
466- "Takes a slice of a byte sequence from start to end. The range is half open, "
467- "[start, end). Indexes can also be negative, indicating indexing from the end of the "
468- "end of the array. By default, start is 0 and end is the length of the buffer. "
469- "Returns a new buffer." )
470- },
471- {
472- "buffer/bit-set" , cfun_buffer_bitset ,
473- JDOC ("(buffer/bit-set buffer index)\n\n"
474- "Sets the bit at the given bit-index. Returns the buffer." )
475- },
476- {
477- "buffer/bit-clear" , cfun_buffer_bitclear ,
478- JDOC ("(buffer/bit-clear buffer index)\n\n"
479- "Clears the bit at the given bit-index. Returns the buffer." )
480- },
481- {
482- "buffer/bit" , cfun_buffer_bitget ,
483- JDOC ("(buffer/bit buffer index)\n\n"
484- "Gets the bit at the given bit-index. Returns true if the bit is set, false if not." )
485- },
486- {
487- "buffer/bit-toggle" , cfun_buffer_bittoggle ,
488- JDOC ("(buffer/bit-toggle buffer index)\n\n"
489- "Toggles the bit at the given bit index in buffer. Returns the buffer." )
490- },
491- {
492- "buffer/blit" , cfun_buffer_blit ,
493- JDOC ("(buffer/blit dest src &opt dest-start src-start src-end)\n\n"
494- "Insert the contents of src into dest. Can optionally take indices that "
495- "indicate which part of src to copy into which part of dest. Indices can be "
496- "negative to index from the end of src or dest. Returns dest." )
497- },
498- {
499- "buffer/format" , cfun_buffer_format ,
500- JDOC ("(buffer/format buffer format & args)\n\n"
501- "Snprintf like functionality for printing values into a buffer. Returns "
502- " the modified buffer." )
503- },
504- {NULL , NULL , NULL }
505- };
506-
507452void janet_lib_buffer (JanetTable * env ) {
508- janet_core_cfuns (env , NULL , buffer_cfuns );
453+ JanetRegExt buffer_cfuns [] = {
454+ JANET_CORE_REG ("buffer/new" , cfun_buffer_new ),
455+ JANET_CORE_REG ("buffer/new-filled" , cfun_buffer_new_filled ),
456+ JANET_CORE_REG ("buffer/fill" , cfun_buffer_fill ),
457+ JANET_CORE_REG ("buffer/trim" , cfun_buffer_trim ),
458+ JANET_CORE_REG ("buffer/push-byte" , cfun_buffer_u8 ),
459+ JANET_CORE_REG ("buffer/push-word" , cfun_buffer_word ),
460+ JANET_CORE_REG ("buffer/push-string" , cfun_buffer_chars ),
461+ JANET_CORE_REG ("buffer/push" , cfun_buffer_push ),
462+ JANET_CORE_REG ("buffer/popn" , cfun_buffer_popn ),
463+ JANET_CORE_REG ("buffer/clear" , cfun_buffer_clear ),
464+ JANET_CORE_REG ("buffer/slice" , cfun_buffer_slice ),
465+ JANET_CORE_REG ("buffer/bit-set" , cfun_buffer_bitset ),
466+ JANET_CORE_REG ("buffer/bit-clear" , cfun_buffer_bitclear ),
467+ JANET_CORE_REG ("buffer/bit" , cfun_buffer_bitget ),
468+ JANET_CORE_REG ("buffer/bit-toggle" , cfun_buffer_bittoggle ),
469+ JANET_CORE_REG ("buffer/blit" , cfun_buffer_blit ),
470+ JANET_CORE_REG ("buffer/format" , cfun_buffer_format ),
471+ JANET_REG_END
472+ };
473+ janet_core_cfuns_ext (env , NULL , buffer_cfuns );
509474}
0 commit comments