Code Monkey home page Code Monkey logo

c_string_buffer's Introduction

stringbuffer

CI Release license

A simple string buffer for C.

Overview

This library provides a simple string buffer which allows adding different basic data types.
It can be used to generate large strings without the need to worry about memory allocation or type conversions.

Usage

#include "stringbuffer.h"
#include <stdio.h>


int main()
{
  // init the buffer with predefined size or with a provided size
  // use stringbuffer_new for predefined size
  struct StringBuffer *buffer = stringbuffer_new_with_options(
    1024, // initial size
    true  // allow resizing
    );

  // different append functions
  stringbuffer_append(buffer, 'A');
  stringbuffer_append_string(buffer, "12345");
  stringbuffer_append_bool(buffer, true);
  stringbuffer_append_short(buffer, 150);
  stringbuffer_append_unsigned_short(buffer, 150);
  stringbuffer_append_int(buffer, 150);
  stringbuffer_append_unsigned_int(buffer, 150);
  stringbuffer_append_long(buffer, 150);
  stringbuffer_append_unsigned_long(buffer, 150);
  stringbuffer_append_long_long(buffer, 150);
  stringbuffer_append_unsigned_long_long(buffer, 150);
  stringbuffer_append_string_with_options(
    buffer,
    "12345",
    1, // offset
    2  // length
    );

  // increase buffer size if needed to ensure capacity
  // and prevent multiple reallocations in case of many
  // append operations
  stringbuffer_ensure_capacity(buffer, 10240);

  // shrink the internal buffer to fit the exact value
  // of the current content and prevent using more memory
  // than needed.
  stringbuffer_shrink(buffer);

  // get a clone text value of the current buffer content
  char *text = stringbuffer_to_string(buffer);
  printf("The Text: %s\n", text);

  // we can clear the content we have so far and reuse the
  // string buffer instance
  stringbuffer_clear(buffer);

  bool is_empty = stringbuffer_is_empty(buffer);
  printf("Is Empty: %d\n", is_empty);

  // once done, free the buffer
  stringbuffer_release(buffer);
} /* main */

Contributing

See contributing guide

Release History

See Changelog

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

c_string_buffer's People

Contributors

sagiegurari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

totto16

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.