Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_function_string.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * afw_function_execute_* functions for String
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /*
19  * Adaptive function: add<string>
20  *
21  * afw_function_execute_add_string
22  *
23  * See afw_function_bindings.h for more information.
24  *
25  * Add (concatenate) 2 or more string values and return the string result.
26  *
27  * This function is pure, so it will always return the same result
28  * given exactly the same parameters and has no side effects.
29  *
30  * Declaration:
31  *
32  * ```
33  * function add<string>(
34  * values_1: string,
35  * values_2: string,
36  * ...values_rest: (list of string)
37  * ): string;
38  * ```
39  *
40  * Parameters:
41  *
42  * values - (2 or more string)
43  *
44  * Returns:
45  *
46  * (string)
47  */
48 const afw_value_t *
51 {
52  const afw_value_string_t **args;
53  const afw_value_string_t * *v;
54  afw_size_t n;
55  afw_size_t len;
58 
59  /* Evaluate args and determine total len and allocate. */
60  args = afw_pool_malloc(x->p, sizeof(afw_value_t *) * x->argc, x->xctx);
61  for (v = args, n = 1, len = 0; n <= x->argc; v++, n++)
62  {
64  if (!*v) {
66  }
67  len += (*v)->internal.len;
68  }
69 
70  /* If length is 0, return empty string. */
71  if (len == 0) {
73  }
74 
75  /* Concatenate strings. */
76  s = afw_pool_calloc(x->p, len, x->xctx);
77  for (v = args, c = s, n = 0; n < x->argc;
78  c += (*v)->internal.len, v++, n++)
79  {
80  memcpy(c, (*v)->internal.s, (*v)->internal.len);
81  }
82 
83  /* Return String value. */
85  afw_utf8_create(s, len, x->p, x->xctx), x->p, x->xctx);
86 }
87 
88 
89 
90 /*
91  * Adaptive function: concat
92  *
93  * afw_function_execute_concat
94  *
95  * See afw_function_bindings.h for more information.
96  *
97  * Convert two or more values of any data type to string and return the
98  * concatenated result. A value with an undefined value is represented by
99  * `<undefined>`.
100  *
101  * This function is pure, so it will always return the same result
102  * given exactly the same parameters and has no side effects.
103  *
104  * Declaration:
105  *
106  * ```
107  * function concat(
108  * values_1: any,
109  * ...values_rest: (list of any)
110  * ): string;
111  * ```
112  *
113  * Parameters:
114  *
115  * values - (1 or more any dataType) Value to convert.
116  *
117  * Returns:
118  *
119  * (string) The concatenated string values.
120  *
121  * Errors thrown:
122  *
123  * cast_error - value could not be converted
124  */
125 const afw_value_t *
128 {
129  const afw_value_string_t **args;
130  const afw_value_string_t * *v;
131  afw_size_t n;
132  afw_size_t len;
133  afw_utf8_octet_t *s;
134  afw_utf8_octet_t *c;
135 
136  /* Evaluate args and determine total len and allocate. */
137  args = afw_pool_malloc(x->p, sizeof(afw_value_t *) * x->argc, x->xctx);
138  for (v = args, n = 1, len = 0; n <= x->argc; v++, n++) {
140  if (!*v) {
142  }
143  len += (*v)->internal.len;
144  }
145 
146  /* If length is 0, return empty string. */
147  if (len == 0) {
148  return afw_value_empty_string;
149  }
150 
151  /* Concatenate strings. */
152  s = afw_pool_calloc(x->p, len, x->xctx);
153  for (v = args, c = s, n = 0; n < x->argc;
154  c += (*v)->internal.len, v++, n++)
155  {
156  memcpy(c, (*v)->internal.s, (*v)->internal.len);
157  }
158 
159  /* Return String value. */
161  afw_utf8_create(s, len, x->p, x->xctx), x->p, x->xctx);
162 }
163 
164 
165 
166 /*
167  * Adaptive function: eq_ignore_case<string>
168  *
169  * afw_function_execute_eq_ignore_case_string
170  *
171  * See afw_function_bindings.h for more information.
172  *
173  * Checks for string arg1 is equal to string arg2 ignoring case and return the
174  * boolean result.
175  *
176  * This function is pure, so it will always return the same result
177  * given exactly the same parameters and has no side effects.
178  *
179  * Declaration:
180  *
181  * ```
182  * function eq_ignore_case<string>(
183  * arg1: string,
184  * arg2: string
185  * ): boolean;
186  * ```
187  *
188  * Parameters:
189  *
190  * arg1 - (string)
191  *
192  * arg2 - (string)
193  *
194  * Returns:
195  *
196  * (boolean)
197  */
198 const afw_value_t *
201 {
202  const afw_value_string_t *arg1;
203  const afw_value_string_t *arg2;
204  int cmp;
205 
208 
210  &arg1->internal, &arg2->internal, x->xctx);
211 
212  return (cmp == 0) ? afw_value_true : afw_value_false;
213 }
214 
215 
216 
217 /*
218  * Adaptive function: normalize_space<string>
219  *
220  * afw_function_execute_normalize_space_string
221  *
222  * See afw_function_bindings.h for more information.
223  *
224  * Remove whitespace from the beginning and end of a string value.
225  *
226  * This function is pure, so it will always return the same result
227  * given exactly the same parameters and has no side effects.
228  *
229  * Declaration:
230  *
231  * ```
232  * function normalize_space<string>(
233  * string: string
234  * ): string;
235  * ```
236  *
237  * Parameters:
238  *
239  * string - (string)
240  *
241  * Returns:
242  *
243  * (string)
244  */
245 const afw_value_t *
248 {
249  const afw_value_string_t *arg;
250  const afw_utf8_t *s;
251 
253 
254  s = afw_utf8_normalize_space(&arg->internal, x->p, x->xctx);
255 
256  /* Return String value. */
257  return afw_value_create_string(s, x->p, x->xctx);
258 }
259 
260 
261 
262 /*
263  * Adaptive function: normalize_to_lower_case<string>
264  *
265  * afw_function_execute_normalize_to_lower_case_string
266  *
267  * See afw_function_bindings.h for more information.
268  *
269  * Normalize string value to lower case and returns string result.
270  *
271  * This function is pure, so it will always return the same result
272  * given exactly the same parameters and has no side effects.
273  *
274  * Declaration:
275  *
276  * ```
277  * function normalize_to_lower_case<string>(
278  * string: string
279  * ): string;
280  * ```
281  *
282  * Parameters:
283  *
284  * string - (string)
285  *
286  * Returns:
287  *
288  * (string)
289  */
290 const afw_value_t *
293 {
294  const afw_value_string_t *arg;
295  const afw_utf8_t *s;
296 
298 
299  s = afw_utf8_to_lower(&arg->internal, x->p, x->xctx);
300 
301  /* Return String value. */
302  return afw_value_create_string(s, x->p, x->xctx);
303 }
304 
305 
306 
307 /*
308  * Adaptive function: string
309  *
310  * afw_function_execute_string
311  *
312  * See afw_function_bindings.h for more information.
313  *
314  * Convert one or more values of any data type to string and return the
315  * concatenated result. A value with an undefined value is represented by
316  * `<undefined>`.
317  *
318  * This function is pure, so it will always return the same result
319  * given exactly the same parameters and has no side effects.
320  *
321  * Declaration:
322  *
323  * ```
324  * function string(
325  * values_1: any,
326  * ...values_rest: (list of any)
327  * ): string;
328  * ```
329  *
330  * Parameters:
331  *
332  * values - (1 or more any dataType) Value to convert.
333  *
334  * Returns:
335  *
336  * (string) The concatenated string values.
337  *
338  * Errors thrown:
339  *
340  * cast_error - value could not be converted
341  */
342 const afw_value_t *
345 {
346  const afw_value_string_t **args;
347  const afw_value_string_t * *v;
348  afw_size_t n;
349  afw_size_t len;
350  afw_utf8_octet_t *s;
351  afw_utf8_octet_t *c;
352 
353  /* Evaluate args and determine total len and allocate. */
354  args = afw_pool_malloc(x->p, sizeof(afw_value_t *) * x->argc, x->xctx);
355  for (v = args, n = 1, len = 0; n <= x->argc; v++, n++) {
357  if (!*v) {
359  }
360  len += (*v)->internal.len;
361  }
362 
363  /* If length is 0, return empty string. */
364  if (len == 0) {
365  return afw_value_empty_string;
366  }
367 
368  /* Concatenate strings. */
369  s = afw_pool_calloc(x->p, len, x->xctx);
370  for (v = args, c = s, n = 0; n < x->argc;
371  c += (*v)->internal.len, v++, n++)
372  {
373  memcpy(c, (*v)->internal.s, (*v)->internal.len);
374  }
375 
376  /* Return String value. */
378  afw_utf8_create(s, len, x->p, x->xctx), x->p, x->xctx);
379 }
380 
381 
382 
383 /*
384  * Adaptive function: url_decode
385  *
386  * afw_function_execute_url_decode
387  *
388  * See afw_function_bindings.h for more information.
389  *
390  * URL decode a value or bag of values.
391  *
392  * This function is pure, so it will always return the same result
393  * given exactly the same parameters and has no side effects.
394  *
395  * Declaration:
396  *
397  * ```
398  * function url_decode(
399  * encoded: string
400  * ): string;
401  * ```
402  *
403  * Parameters:
404  *
405  * encoded - (string) URL decode a single string or a bag of string.
406  *
407  * Returns:
408  *
409  * (string) A string or bag of strings.
410  */
411 const afw_value_t *
414 {
416  AFW_THROW_ERROR_Z(general, "Not implemented", x->xctx);
417 }
Adaptive Framework Core Internal.
afw_value_create_string(const afw_utf8_t *internal, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for unmanaged data type string value.
char afw_utf8_octet_t
8 bits of utf-8 codepoint.
Definition: afw_common.h:236
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define AFW_FUNCTION_EVALUATE_DATA_TYPE_PARAMETER(A_RESULT, A_N, A_TYPE)
Evaluate an arg for a particular data type.
Definition: afw_function.h:261
#define AFW_FUNCTION_EVALUATE_REQUIRED_DATA_TYPE_PARAMETER(A_RESULT, A_N, A_TYPE)
Evaluate an arg for a particular data type.
Definition: afw_function.h:328
const afw_value_t * afw_function_execute_normalize_to_lower_case_string(afw_function_execute_t *x)
Adaptive Function normalize_to_lower_case<string>
const afw_value_t * afw_function_execute_normalize_space_string(afw_function_execute_t *x)
Adaptive Function normalize_space<string>
const afw_value_t * afw_function_execute_string(afw_function_execute_t *x)
Adaptive Function string
const afw_value_t * afw_function_execute_eq_ignore_case_string(afw_function_execute_t *x)
Adaptive Function eq_ignore_case<string>
const afw_value_t * afw_function_execute_add_string(afw_function_execute_t *x)
Adaptive Function add<string>
const afw_value_t * afw_function_execute_concat(afw_function_execute_t *x)
Adaptive Function concat
const afw_value_t * afw_function_execute_url_decode(afw_function_execute_t *x)
Adaptive Function url_decode
#define afw_pool_malloc(instance, size, xctx)
Call method malloc of interface afw_pool.
#define afw_pool_calloc(instance, size, xctx)
Call method calloc of interface afw_pool.
int afw_utf8_compare_ignore_case(const afw_utf8_t *s1, const afw_utf8_t *s2, afw_xctx_t *xctx)
Compare two strings ignoring case.
const afw_utf8_t * afw_utf8_normalize_space(const afw_utf8_t *s, const afw_pool_t *p, afw_xctx_t *xctx)
Create a utf-8 sting with spaces normalized in specified pool.
const afw_utf8_t * afw_utf8_to_lower(const afw_utf8_t *s, const afw_pool_t *p, afw_xctx_t *xctx)
Convert utf-8 sting to lower case in specified pool.
#define afw_utf8_create(s, len, p, xctx)
Create utf-8 string without copy unless necessary in pool specified.
Definition: afw_utf8.h:239
afw_value_undefined_as_string
Adaptive value containing <undefined> string.
Definition: afw_value.h:336
afw_value_false
Adaptive value false.
Definition: afw_value.h:354
afw_value_empty_string
Adaptive value empty string.
Definition: afw_value.h:342
afw_value_true
Adaptive value true.
Definition: afw_value.h:348
Function execute parameter.
Definition: afw_function.h:53
afw_xctx_t * xctx
The execution context (xctx) of caller.
Definition: afw_function.h:62
const afw_pool_t * p
Pool for result.
Definition: afw_function.h:59
afw_size_t argc
This is the argv count not counting argv[0].
Definition: afw_function.h:89
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
struct for data type string values.