Skip to content

Commit 3d3a800

Browse files
akosthekissLaszloLango
authored andcommitted
Don't use strlen for string literals (#2517)
Their length (size) is known at compile time. Therefore `sizeof` is more efficient for them. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 2d83d8e commit 3d3a800

24 files changed

+368
-390
lines changed

docs/02.API-REFERENCE.md

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -783,15 +783,14 @@ jerry_run_simple (const jerry_char_t *script_source_p,
783783
[doctest]: # ()
784784

785785
```c
786-
#include <string.h>
787786
#include "jerryscript.h"
788787

789788
int
790789
main (void)
791790
{
792-
const jerry_char_t *script = (const jerry_char_t *) "print ('Hello, World!');";
791+
const jerry_char_t script[] = "print ('Hello, World!');";
793792

794-
jerry_run_simple (script, strlen ((const char *) script), JERRY_INIT_EMPTY);
793+
jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
795794
}
796795
```
797796

@@ -839,7 +838,6 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
839838
[doctest]: # ()
840839

841840
```c
842-
#include <string.h>
843841
#include "jerryscript.h"
844842

845843
int
@@ -848,9 +846,8 @@ main (void)
848846
jerry_init (JERRY_INIT_EMPTY);
849847

850848
const jerry_char_t script[] = "print ('Hello, World!');";
851-
size_t script_size = strlen ((const char *) script);
852849

853-
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
850+
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
854851
jerry_release_value (parsed_code);
855852

856853
jerry_cleanup ();
@@ -924,20 +921,18 @@ jerry_run (const jerry_value_t func_val);
924921
[doctest]: # ()
925922

926923
```c
927-
#include <string.h>
928924
#include "jerryscript.h"
929925

930926
int
931927
main (void)
932928
{
933929
const jerry_char_t script[] = "print ('Hello, World!');";
934-
size_t script_size = strlen ((const char *) script);
935930

936931
/* Initialize engine */
937932
jerry_init (JERRY_INIT_EMPTY);
938933

939934
/* Setup Global scope code */
940-
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
935+
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
941936

942937
if (!jerry_value_is_error (parsed_code))
943938
{
@@ -1016,7 +1011,6 @@ jerry_run_all_enqueued_jobs (void)
10161011
[doctest]: # ()
10171012

10181013
```c
1019-
#include <string.h>
10201014
#include "jerryscript.h"
10211015

10221016
int
@@ -1025,9 +1019,8 @@ main (void)
10251019
jerry_init (JERRY_INIT_EMPTY);
10261020

10271021
const jerry_char_t script[] = "new Promise(function(f,r) { f('Hello, World!'); }).then(function(x) { print(x); });";
1028-
size_t script_size = strlen ((const char *) script);
10291022

1030-
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
1023+
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
10311024
jerry_value_t script_value = jerry_run (parsed_code);
10321025
jerry_value_t job_value = jerry_run_all_enqueued_jobs ();
10331026

@@ -2989,10 +2982,10 @@ jerry_create_error_sz (jerry_error_t error_type,
29892982

29902983
```c
29912984
{
2992-
const jerry_char_t *message = "error";
2985+
const jerry_char_t message[] = "error";
29932986
jerry_value_t error_obj = jerry_create_error_sz (JERRY_ERROR_COMMON,
29942987
message,
2995-
strlen ((const char *) message));
2988+
sizeof (message) - 1);
29962989

29972990
... // usage of error_obj
29982991

@@ -3320,7 +3313,7 @@ jerry_create_string_sz (const jerry_char_t *str_p,
33203313
{
33213314
const jerry_char_t char_array[] = "a string";
33223315
jerry_value_t string_value = jerry_create_string_sz (char_array,
3323-
strlen ((const char *) char_array));
3316+
sizeof (char_array) - 1);
33243317

33253318
... // usage of string_value
33263319

@@ -3398,7 +3391,7 @@ jerry_create_string_sz (const jerry_char_t *str_p,
33983391
{
33993392
const jerry_char_t char_array[] = "a string";
34003393
jerry_value_t string_value = jerry_create_string_sz_from_utf8 (char_array,
3401-
strlen ((const char *) char_array));
3394+
sizeof (char_array) - 1);
34023395

34033396
... // usage of string_value
34043397

@@ -4792,16 +4785,15 @@ jerry_is_valid_utf8_string (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
47924785
[doctest]: # ()
47934786

47944787
```c
4795-
#include <string.h>
47964788
#include "jerryscript.h"
47974789

47984790
int
47994791
main (void)
48004792
{
48014793
const jerry_char_t script[] = "print ('Hello, World!');";
4802-
size_t script_size = strlen ((const char *) script);
4794+
const jerry_size_t script_size = sizeof (script) - 1;
48034795

4804-
if (jerry_is_valid_utf8_string (script, (jerry_size_t) script_size))
4796+
if (jerry_is_valid_utf8_string (script, script_size))
48054797
{
48064798
jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
48074799
}
@@ -4840,7 +4832,6 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
48404832
[doctest]: # ()
48414833

48424834
```c
4843-
#include <string.h>
48444835
#include "jerryscript.h"
48454836

48464837
int
@@ -4849,12 +4840,12 @@ main (void)
48494840
jerry_init (JERRY_INIT_EMPTY);
48504841

48514842
const jerry_char_t script[] = "Hello, World!";
4852-
size_t script_size = strlen ((const char *) script);
4843+
const jerry_size_t script_size = sizeof (script) - 1;
48534844

4854-
if (jerry_is_valid_cesu8_string (script, (jerry_size_t) script_size))
4845+
if (jerry_is_valid_cesu8_string (script, script_size))
48554846
{
48564847
jerry_value_t string_value = jerry_create_string_sz (script,
4857-
(jerry_size_t) script_size);
4848+
script_size);
48584849

48594850
// usage of string_value
48604851

@@ -5059,7 +5050,6 @@ jerry_generate_snapshot (const jerry_char_t *resource_name_p,
50595050
[doctest]: # ()
50605051

50615052
```c
5062-
#include <string.h>
50635053
#include "jerryscript.h"
50645054

50655055
int
@@ -5068,13 +5058,13 @@ main (void)
50685058
jerry_init (JERRY_INIT_EMPTY);
50695059

50705060
static uint32_t global_mode_snapshot_buffer[256];
5071-
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
5061+
const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
50725062

50735063
jerry_value_t generate_result;
50745064
generate_result = jerry_generate_snapshot (NULL,
50755065
0,
5076-
code_to_snapshot_p,
5077-
strlen ((const char *) code_to_snapshot_p),
5066+
script_to_snapshot,
5067+
sizeof (script_to_snapshot) - 1,
50785068
0,
50795069
global_mode_snapshot_buffer,
50805070
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -5139,7 +5129,6 @@ jerry_generate_function_snapshot (const jerry_char_t *resource_name_p,
51395129
[doctest]: # ()
51405130

51415131
```c
5142-
#include <string.h>
51435132
#include "jerryscript.h"
51445133

51455134
int
@@ -5148,16 +5137,16 @@ main (void)
51485137
jerry_init (JERRY_INIT_EMPTY);
51495138

51505139
static uint32_t func_snapshot_buffer[256];
5151-
const jerry_char_t *args_p = (const jerry_char_t *) "a, b";
5152-
const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;";
5140+
const jerry_char_t args[] = "a, b";
5141+
const jerry_char_t src[] = "return a + b;";
51535142

51545143
jerry_value_t generate_result;
51555144
generate_result = jerry_generate_function_snapshot (NULL,
51565145
0,
5157-
src_p,
5158-
strlen ((const char *) src_p),
5159-
args_p,
5160-
strlen ((const char *) args_p),
5146+
src,
5147+
sizeof (src) - 1,
5148+
args,
5149+
sizeof (args) - 1,
51615150
0,
51625151
func_snapshot_buffer,
51635152
sizeof (func_snapshot_buffer) / sizeof (uint32_t));
@@ -5209,22 +5198,21 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
52095198
[doctest]: # ()
52105199

52115200
```c
5212-
#include <string.h>
52135201
#include "jerryscript.h"
52145202

52155203
int
52165204
main (void)
52175205
{
52185206
static uint32_t global_mode_snapshot_buffer[256];
5219-
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
5207+
const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
52205208

52215209
jerry_init (JERRY_INIT_EMPTY);
52225210

52235211
jerry_value_t generate_result;
52245212
generate_result = jerry_generate_snapshot (NULL,
52255213
0,
5226-
code_to_snapshot_p,
5227-
strlen ((const char *) code_to_snapshot_p),
5214+
script_to_snapshot,
5215+
sizeof (script_to_snapshot) - 1,
52285216
0,
52295217
global_mode_snapshot_buffer,
52305218
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -5287,25 +5275,24 @@ jerry_load_function_snapshot (const uint32_t *snapshot_p,
52875275
[doctest]: # ()
52885276

52895277
```c
5290-
#include <string.h>
52915278
#include "jerryscript.h"
52925279

52935280
int
52945281
main (void)
52955282
{
52965283
static uint32_t snapshot_buffer[256];
5297-
const jerry_char_t *args_p = (const jerry_char_t *)"a, b";
5298-
const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;";
5284+
const jerry_char_t func_args[] = "a, b";
5285+
const jerry_char_t func_src[] = "return a + b;";
52995286

53005287
jerry_init (JERRY_INIT_EMPTY);
53015288

53025289
jerry_value_t generate_result;
53035290
generate_result = jerry_generate_function_snapshot (NULL,
53045291
0,
5305-
src_p,
5306-
strlen ((const char *) src_p),
5307-
args_p,
5308-
strlen ((const char *) args_p),
5292+
func_src,
5293+
sizeof (func_src) - 1,
5294+
func_args,
5295+
sizeof (func_args) - 1,
53095296
false,
53105297
snapshot_buffer,
53115298
sizeof (snapshot_buffer) / sizeof (uint32_t));
@@ -5383,7 +5370,6 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p,
53835370

53845371
```c
53855372
#include <stdio.h>
5386-
#include <string.h>
53875373
#include "jerryscript.h"
53885374

53895375
int
@@ -5393,13 +5379,12 @@ main (void)
53935379

53945380
static jerry_char_t literal_buffer[256];
53955381
static uint32_t snapshot_buffer[256];
5396-
const jerry_char_t *code_for_literal_save_p = (const jerry_char_t *) "var obj = { a:'aa', bb:'Bb' }";
5397-
size_t code_for_literal_save_size = strlen ((const char *) code_for_literal_save_p);
5382+
const jerry_char_t script_for_literal_save[] = "var obj = { a:'aa', bb:'Bb' }";
53985383

53995384
jerry_value_t generate_result = jerry_generate_snapshot (NULL,
54005385
0,
5401-
code_for_literal_save_p,
5402-
code_for_literal_save_size,
5386+
script_for_literal_save,
5387+
sizeof (script_for_literal_save) - 1,
54035388
0,
54045389
snapshot_buffer,
54055390
256);
@@ -5471,7 +5456,6 @@ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
54715456
[doctest]: # (test="link")
54725457

54735458
```c
5474-
#include <string.h>
54755459
#include "jerryscript.h"
54765460

54775461
static int countdown = 10;
@@ -5497,11 +5481,11 @@ main (void)
54975481
jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
54985482

54995483
// Inifinte loop.
5500-
const char *src_p = "while(true) {}";
5484+
const jerry_char_t script[] = "while(true) {}";
55015485

5502-
jerry_value_t src = jerry_parse (NULL, 0, (jerry_char_t *) src_p, strlen (src_p), JERRY_PARSE_NO_OPTS);
5503-
jerry_release_value (jerry_run (src));
5504-
jerry_release_value (src);
5486+
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
5487+
jerry_release_value (jerry_run (parsed_code));
5488+
jerry_release_value (parsed_code);
55055489
jerry_cleanup ();
55065490
}
55075491
```
@@ -5920,9 +5904,8 @@ jerry_value_t jerry_json_parse (const jerry_char_t *string_p, jerry_size_t strin
59205904

59215905
```c
59225906
{
5923-
const char *data = "{\"name\": \"John\", \"age\": 5}";
5924-
jerry_size_t str_length = (jerry_size_t)strlen (data);
5925-
jerry_value_t parsed_json = jerry_json_parse ((jerry_char_t*)data, str_length);
5907+
const jerry_char_t data[] = "{\"name\": \"John\", \"age\": 5}";
5908+
jerry_value_t parsed_json = jerry_json_parse (data, sizeof (data) - 1);
59265909

59275910
// parsed_json now conatins all data stored in data_in_json
59285911

0 commit comments

Comments
 (0)