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 akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2018-09-17 10:07:24 +02:00
committed by László Langó
parent 2d83d8ed17
commit 3d3a8008a8
24 changed files with 367 additions and 389 deletions
+10 -23
View File
@@ -7,16 +7,14 @@ This guide is intended to introduce you to JerryScript embedding API through cre
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
const jerry_char_t script[] = "var str = 'Hello, World!';";
size_t script_size = strlen ((const char *) script);
bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
bool ret_value = jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
return (ret_value ? 0 : 1);
}
@@ -37,7 +35,6 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
@@ -45,7 +42,6 @@ int
main (void)
{
const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script);
/* Initialize engine */
jerry_init (JERRY_INIT_EMPTY);
@@ -55,7 +51,7 @@ main (void)
jerryx_handler_print);
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
if (!jerry_value_is_error (parsed_code))
{
@@ -83,7 +79,6 @@ Our code is more complex now, but it introduces possibilities to interact with J
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
@@ -104,7 +99,7 @@ main (void)
/* Evaluate script1 */
eval_ret = jerry_eval (script_1,
strlen ((const char *) script_1),
sizeof (script_1) - 1,
JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */
@@ -112,7 +107,7 @@ main (void)
/* Evaluate script2 */
eval_ret = jerry_eval (script_2,
strlen ((const char *) script_2),
sizeof (script_2) - 1,
JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */
@@ -132,7 +127,6 @@ This way, we execute two independent script parts in one execution environment.
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
@@ -168,7 +162,7 @@ main (void)
/* Now starting script that would output value of just initialized field */
jerry_value_t eval_ret = jerry_eval (script,
strlen ((const char *) script),
sizeof (script) - 1,
JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */
@@ -197,7 +191,6 @@ The following example function will output a JavaScript value:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerryscript.h"
static void
@@ -312,7 +305,7 @@ main (void)
}
/* If the command is "quit", break the loop */
if (!strncmp (cmd, "quit\n", strlen ("quit\n")))
if (!strncmp (cmd, "quit\n", sizeof ("quit\n") - 1))
{
break;
}
@@ -352,7 +345,6 @@ In this example we demonstrate how to use native function and structures in Java
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
@@ -417,10 +409,9 @@ main (void)
var str = MyObject.myFunc (); \
print (str); \
";
size_t script_size = strlen ((const char *) script);
/* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
@@ -445,7 +436,6 @@ Here we create a JS Object with `jerry_eval`, then extend it with a native funct
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
@@ -508,7 +498,7 @@ main (void)
/* Evaluate script */
my_js_obj_val = jerry_eval (my_js_object,
strlen ((const char *) my_js_object),
sizeof (my_js_object) - 1,
JERRY_PARSE_NO_OPTS);
/* Create a JS function object and wrap into a jerry value */
@@ -529,10 +519,9 @@ main (void)
MyObject.add2x (5); \
print (MyObject.foo ()); \
";
size_t script_size = strlen ((const char *) script);
/* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
@@ -559,7 +548,6 @@ A recommended method is using `jerry_port_get_current_time()` or something based
[doctest]: # ()
```c
#include <string.h>
#include <stdlib.h>
#include "jerryscript.h"
#include "jerryscript-port.h"
@@ -573,7 +561,6 @@ main (void)
/* Generate a random number, and print it */
const jerry_char_t script[] = "var a = Math.random (); print(a)";
size_t script_size = strlen ((const char *) script);
/* Initialize the engine */
jerry_init (JERRY_INIT_EMPTY);
@@ -583,7 +570,7 @@ main (void)
jerryx_handler_print);
/* Evaluate the script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free the JavaScript value returned by eval */
jerry_release_value (eval_ret);