Skip to content

Commit 97303eb

Browse files
robertsipkayichoi
authored andcommitted
Add API documentation for jerry_create_string_from_utf8 and jerry_create_string_sz_from_utf8 functions. (#1444)
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent cf7b7a1 commit 97303eb

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

docs/02.API-REFERENCE.md

+74
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,80 @@ jerry_create_string_sz (const jerry_char_t *str_p,
19811981

19821982
- [jerry_create_string](#jerry_create_string)
19831983

1984+
## jerry_create_string_from_utf8
1985+
1986+
**Summary**
1987+
1988+
Create string from a valid UTF8 string.
1989+
1990+
*Note*: The difference from [jerry_create_string](#jerry_create_string) is that it accepts utf-8 string instead of cesu-8 string.
1991+
1992+
**Prototype**
1993+
1994+
```c
1995+
jerry_value_t
1996+
jerry_create_string_from_utf8 (const jerry_char_t *str_p);
1997+
```
1998+
1999+
- `str_p` - pointer to string
2000+
- return value - value of the created string
2001+
2002+
**Example**
2003+
2004+
```c
2005+
{
2006+
const jerry_char_t char_array[] = "a string";
2007+
jerry_value_t string_value = jerry_create_string_from_utf8 (char_array);
2008+
2009+
... // usage of string_value
2010+
2011+
jerry_release_value (string_value);
2012+
}
2013+
```
2014+
2015+
**See also**
2016+
2017+
- [jerry_create_string_sz_from_utf8](#jerry_create_string_sz_from_utf8)
2018+
2019+
2020+
## jerry_create_string_sz_from_utf8
2021+
2022+
**Summary**
2023+
2024+
Create string from a valid UTF8 string.
2025+
2026+
*Note*: The difference from [jerry_create_string_sz](#jerry_create_string_sz) is that it accepts utf-8 string instead of cesu-8 string.
2027+
2028+
**Prototype**
2029+
2030+
```c
2031+
jerry_value_t
2032+
jerry_create_string_sz (const jerry_char_t *str_p,
2033+
jerry_size_t str_size)
2034+
```
2035+
2036+
- `str_p` - pointer to string
2037+
- `str_size` - size of the string
2038+
- return value - value of the created string
2039+
2040+
**Example**
2041+
2042+
```c
2043+
{
2044+
const jerry_char_t char_array[] = "a string";
2045+
jerry_value_t string_value = jerry_create_string_sz_from_utf8 (char_array,
2046+
strlen ((const char *) char_array));
2047+
2048+
... // usage of string_value
2049+
2050+
jerry_release_value (string_value);
2051+
}
2052+
2053+
```
2054+
2055+
**See also**
2056+
2057+
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
19842058

19852059
## jerry_create_undefined
19862060

tests/unit/test-api.c

+18
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,24 @@ main (void)
339339
TEST_ASSERT (sz == 0);
340340
jerry_release_value (args[0]);
341341

342+
// Test create_jerry_string_from_utf8 with 4-byte long unicode sequences
343+
args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a\xf0\x90\x90\x80");
344+
args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a\xed\xa0\x81\xed\xb0\x80");
345+
346+
jerry_size_t utf8_sz = jerry_get_string_size (args[0]);
347+
jerry_size_t cesu8_sz = jerry_get_string_size (args[1]);
348+
349+
char string_from_utf8[utf8_sz];
350+
char string_from_cesu8[cesu8_sz];
351+
352+
jerry_string_to_char_buffer (args[1], (jerry_char_t *) string_from_utf8, utf8_sz);
353+
jerry_string_to_char_buffer (args[1], (jerry_char_t *) string_from_cesu8, cesu8_sz);
354+
355+
TEST_ASSERT (utf8_sz == cesu8_sz);
356+
TEST_ASSERT (!strncmp (string_from_utf8, string_from_cesu8, utf8_sz));
357+
jerry_release_value (args[0]);
358+
jerry_release_value (args[1]);
359+
342360
// Get global.boo (non-existing field)
343361
val_t = get_property (global_obj_val, "boo");
344362
TEST_ASSERT (!jerry_value_has_error_flag (val_t));

0 commit comments

Comments
 (0)