Strings

Table of Contents

Stringshare

The Eina_Stringshare data type functions allow you to store a single copy of a string and use it in multiple places throughout your program. This way you can save a lot of strings with less memory. It improves string creation and destruction speed, reduces memory use, and decreases memory fragmentation.

With this data type you can reduce the number of duplicated strings kept in memory. It is common for the same strings to be dynamically allocated repeatedly between applications and libraries, especially in circumstances where you can have multiple copies of a structure that allocates the string. Rather than duplicating and freeing these strings, request a read-only pointer to an existing string and only incur the overhead of a hash lookup. This can sound like micro-optimizing, but profiling has shown that this can have a significant impact as the number of copies grows.

To manage stringshares

1. To create a stringshare, declare a string variable and call the eina_stringshare_add() function:

const char *mystr;
const char *prologue = "Enlightenment is not just a window manager for Linux/X11 and others"
 
mystr = eina_stringshare_add(prologue);

2. To retrieve or modify the string data:

  • Retrieve a string for use in a program from a format string using the eina_stringshare_printf() function. If you have a “format” string to pass to a function like printf, you can store it as a stringshare as well.
    The following example produces “1 desktop manager to rule them all”.
const char *myfmtstr = "%d desktop manager to rule them all";
const char *str;
 
str = eina_stringshare_printf(myfmtstr, 1);
 
print(str)
  • Replace the value of a stringshare with the eina_stringshare_replace() function. Pass the pointer address and the new value to the function.
eina_stringshare_replace(&str,"One desktop manager to rule them all");
  • Retrieve the length of the stringshare value with the eina_stringshare_strlen() function.
    printf("length: %d\n", eina_stringshare_strlen(str));

3. When the string is no longer needed, delete it using the eina_stringshare_del() function:

eina_stringshare_del(mystr);

String Buffer

The string buffer data type is designed to be a mutable string, allowing you to append, prepend or insert a string to a buffer. It allows easy handling of buffers in your applications.

To manage string buffer

1. Initialize the Eina_Strbuf instance and create the buffer:

Eina_Strbuf *buf;
mybuffer = eina_strbuf_new();

2. Manage the buffer content:

  • To append characters to the buffer:

For basic strings, use the eina_strbuf_append() function:

eina_strbuf_append(mybuffer, "This is my string.");

To append 1 character to your buffer, use the eina_strbuf_append_char() function. You can also append a sized string to the buffer using the eina_strbuf_append_length() function:

eina_strbuf_append_length(mybuffer, "Buffe", 5);
eina_strbuf_append_char(mybuffer, 'r');

To handle “printf” format strings, use the eina_strbuf_append_printf() function to add formatted strings to the buffer:

eina_strbuf_append_printf(buf, "%s%c", "buffe", 'r');
  • To remove characters from one position to another, use the eina_strbuf_remove() function. The first parameter is the buffer, the second is the start position of the characters you want to delete, and the last the end position.
    This example removes the first 19 characters of the buffer:
eina_strbuf_remove(buf, 0, 18);
  • To replace characters:
    • eina_strbuf_replace() replaces a specific occurrence of a given string in the buffer with another string.
    • eina_strbuf_replace_all() replaces all occurrences of a given string in the buffer with another string.
eina_strbuf_append(mybuffer, "buffer buffer buffer");
 
// Replacing one occurrence of "buffer" by "B-U-F-F-E-R"
eina_strbuf_replace(mybuffer, "buffer", "B-U-F-F-E-R", 1);
 
// Replacing all the occurrences of "buffer" by "B-U-F-F-E-R"
eina_strbuf_replace_all(mybuffer, "buffer", "B-U-F-F-E-R");
 
// Replacing all the occurrences of "B-U-F-F-E-R" by "Buffer"
eina_strbuf_replace_all(mybuffer, "B-U-F-F-E-R", "Buffer");
  • To insert a string at the specified position, use the eina_strbuf_insert() function. Use the eina_strbuf_insert_printf() function with formatted strings.
eina_strbuf_insert(mybuffer, "More buffer", 10);
 
// Using eina_strbuf_length_get to get the buffer length
eina_strbuf_insert_printf(buf, " %s: %d", 6, "length", eina_strbuf_length_get(buf));
  • To get the complete length of the string and the buffer, use the eina_strbuf_string_get() and eina_strbuf_length_get() functions:
printf("%s : %d\n", eina_strbuf_string_get(mybuffer), eina_strbuf_length_get(buf));

3. When no longer needed, free the buffer with the eina_strbuf_free() function. You can also free the content of Eina_Strbuf without freeing the buffer itself using the eina_strbuf_string_free() function.

eina_strbuf_free(mybuffer);