Table of Contents

Eina Menu


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:

const char *myfmtstr = "%d desktop manager to rule them all";
const char *str;
 
str = eina_stringshare_printf(myfmtstr, 1);
 
print(str)
eina_stringshare_replace(&str,"One desktop manager to rule them all");
    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:

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');
eina_strbuf_remove(buf, 0, 18);
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");
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));
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);

Eina Menu