summaryrefslogtreecommitdiff
path: root/reference/c/eina/src/eina_string.c
blob: 43e7f3ce2cdb1aa8f3d9efe31986e3398b5d947d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <stdio.h>

#include <Eina.h>
#include <Efl_Core.h>

/*
 * Eina String examples.
 *
 * These examples demonstrate how to work with eina_str, eina_strbuf and
 * eina_stringshare. The eina_str_* methods are helper functions for managing
 * strings, eina_stringshare provides methods for more easily managing the
 * memory needed by strings, saving space and speeding up operations.
 * Lastly eina_strbuf is an API for mutating strings optimally which can then
 * provide stanard string exports to work with stanard string functions.
 */

static void
_string_splitjoin()
{
   const char *names = "Calvin;Leoben;D'anna;Simon;Doral;Six;Daniel;Sharon";
   char **arr;
   char *joined;
   int i;

   // split the string into an array on each occurrence of ";"
   printf("Name list split\n");
   arr = eina_str_split(names, ";", 0);
   for (i = 0; arr[i]; i++)
     printf(" %s\n", arr[i]);

   // Here we re-join two of the strings with a different separator - "&"
   joined = malloc(sizeof(char) * 11);
   eina_str_join_len(joined, 11, '&', arr[0], strlen(arr[0]),
                     arr[1], strlen(arr[1]));
   printf("Joined limit %s\n", joined);

   free(joined);
   // To free this array we only need to free the first item and the array
   free(arr[0]);
   free(arr);
}

static void
_string_case()
{
   char *str;

   str = strdup("Welcome");
   printf("String: %s\n", str);

   eina_str_toupper(&str);
   printf(" Upper: %s\n", str);

   eina_str_tolower(&str);
   printf(" Lower: %s\n", str);
   free(str);
}

static void
_string_startend()
{
   const char *file = "libefl.so";
   char *start;

   printf("File named %s:\n", file);
   printf(" starts with \"lib\"? %d\n", eina_str_has_prefix(file, "lib"));
   printf(" ends with \"efl.SO\"? %d\n", eina_str_has_suffix(file, "efl.SO"));

   // The has_extension method is like has_suffix but case insensitive
   printf(" has extension \".SO\"? %d\n", eina_str_has_extension(file, ".SO"));

   // we can also copy segments of a string
   start = malloc(sizeof(char) * 7);
   eina_strlcpy(start, file, 7);
   printf(" first 6 chars \"%s\"\n", start);
   free(start);
}

static void
_string_escape()
{
   const char *str;
   char *tmp;

   str = "Here's some \"example\" text.";

   // backslash escaping a string for use on the command line, for example
   tmp = eina_str_escape(str);
   printf("String: %s\n", str);
   printf("Escaped: %s\n", tmp);
   free(tmp);
}

static void
_string_time()
{
   char *time_arr;
   time_t curr_time;
   struct tm *info;

   curr_time = time(NULL);
   info = localtime(&curr_time);
   time_arr = eina_strftime("%d/%m/%Y", info);
   printf("Today's Date: %s\n", time_arr);
   free(time_arr);
}

static void
_string_share()
{
   const char *str;
   const char *line1 = "The Cylons were created by man. They rebelled. They "
                       "evolved.";
   const char *line2 = "%d Cylon models. %d are known. %d live in secret. "
                       "%s will be revealed.";
   const char *line3 = "There are many copies. And they have a plan.";

   // Creating a new stringshare which is deleted when we are done
   // If other methods had referenced the string it would not delete immediately
   printf("Line1: %s\n", line1);
   str = eina_stringshare_add_length(line1, 31);
   printf(" limited to %d: %s\n", eina_stringshare_strlen(str), str);
   eina_stringshare_del(str);

   // There are many helpful printf functions for formatting shared strings
   str = eina_stringshare_printf(line2, 12, 3, 4, "four");
   printf("Line2: %s\n", str);
   eina_stringshare_del(str);
   str = eina_stringshare_nprintf(48, line2, 12, 7, 5, "four");
   printf(" format limit (len %d): %s...\n", eina_stringshare_strlen(str), str);
   eina_stringshare_del(str);

   // By replacing a stringshare it applies anywhere that references this share
   printf("Line3: %s\n", line3);
   str = eina_stringshare_add(line3);
   printf(" shared: %s\n", str);
   eina_stringshare_replace(&str, line1);
   printf(" replaced: %s\n", str);
   eina_stringshare_del(str);
}

static void
_string_buf()
{
   Eina_Strbuf *buf, *substr;

   // populate a new bugger
   buf = eina_strbuf_new();
   eina_strbuf_append_length(buf, "BUFFE", 5);
   eina_strbuf_append_char(buf, 'R');
   printf("buffer: %s\n", eina_strbuf_string_get(buf));

   // Demonstrating standard string functions using a strbuf
   eina_strbuf_tolower(buf);
   printf("lower: %s\n", eina_strbuf_string_get(buf));

   substr = eina_strbuf_substr_get(buf, 3, 2);
   printf("substr: %s\n", eina_strbuf_string_get(substr));
   eina_strbuf_free(substr);

   eina_strbuf_insert_escaped(buf, "my ", 0);
   printf("escaped: %s\n", eina_strbuf_string_get(buf));
   eina_strbuf_reset(buf);
   printf("\n");

   // we can even insert or remove content within the string buffer
   eina_strbuf_append_printf(buf, "%s%c", "buffe", 'r');
   eina_strbuf_insert_printf(buf, " %s: %d", 6, "length", (int)eina_strbuf_length_get(buf));
   printf("printf: %s\n", eina_strbuf_string_get(buf));

   eina_strbuf_remove(buf, 7, 15);
   printf("removed: %s\n", eina_strbuf_string_get(buf));

   eina_strbuf_replace_all(buf, "buffer", "size");
   printf("replaced: %s\n", eina_strbuf_string_get(buf));
   eina_strbuf_free(buf);
   printf("\n");

   // Let's manage a new buffer with a readonly string (i.e. not malloc'd)
   buf = eina_strbuf_manage_read_only_new_length("Example string", 14);
   printf("manage: %s\n", eina_strbuf_string_get(buf));
   // We can still mutate as internals deal with readonly -> write
   eina_strbuf_append_char(buf, '.');
   printf("append: %s\n", eina_strbuf_string_get(buf));

   eina_strbuf_free(buf);
}

static void
_string_buf_time()
{
   Eina_Strbuf *buf;
   time_t curr_time;
   struct tm *info;

   curr_time = time(NULL);
   info = localtime(&curr_time);

   buf = eina_strbuf_new();
   eina_strbuf_append_strftime(buf, "%I:%M%p", info);
   printf("current time: %s\n", eina_strbuf_string_get(buf));
   eina_strbuf_reset(buf);

   eina_strbuf_append(buf, "Hours, Minutes");
   // Add hours at the beginning
   eina_strbuf_prepend_strftime(buf, "%I ", info);
   // Insert minutes at the right position
   eina_strbuf_insert_strftime(buf, "%M ", info, 10);
   printf("%s\n", eina_strbuf_string_get(buf));

   eina_strbuf_free(buf);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   _string_splitjoin();
   printf("\n");

   _string_case();
   printf("\n");

   _string_startend();
   printf("\n");

   _string_escape();
   printf("\n");

   _string_time();
   printf("\n");

   _string_share();
   printf("\n");

   _string_buf();
   printf("\n");
   _string_buf_time();

   efl_exit(0);
}
EFL_MAIN()