You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be useful if the ReplaceAll method could accept an array.
As an example, I altered the ReplaceAll code to produce this:
// from javascript call as: sb.replaceFromArray()
napi_value ReplaceFromArray(napi_env env, napi_callback_info info){
size_t argsLength = 1;
napi_value args[2]; // will hold parameters from a 2d array during a for loop
napi_value me;
int n;
// test with hard-coded array containing 2 pairs of strings
// "john is some writer" => "john is this joke"
const char* arr[][2] = {{"some", "this"},
{"writer", "joke"}};
napi_get_cb_info(env, info, &argsLength, args, &me, 0); // place input parameters into args[]
/* if (argsLength < 2){ // this bit no longer needed
return me;
*/
uint16_t* buffer;
int64_t* metadata;
getBufferAndMetaData(env, me, &buffer, &metadata);
uint16_t* pattern;
int64_t patternLength;
bool patternFreeAble;
for (n = 0; n < 2; n++ )
{
napi_create_string_utf8(env, arr[n][0],
NAPI_AUTO_LENGTH ,
&args[0]);
napi_create_string_utf8(env, arr[n][1],
NAPI_AUTO_LENGTH ,
&args[1]);
// ..... the rest of the replaceAll code
}
return me;
}
I also note that the main section from replaceAll is mostly identical to that inside the ReplacePattern function. There is a potential therefore for refactoring, by which this section could be called from a separate function.
A more sophisticated implementation of ReplaceFromArray() would pass the array as a parameter, although embedding a hard-coded array has the advantage of "hiding" the array contents, while still suiting many possible applications.
The text was updated successfully, but these errors were encountered:
Sure. But you'd be entering replaceAll from javascript each time, with
attendant overhead. So maybe quicker to keep the loop inside replaceAll?
Also, repeating the loop in javascript keeps the data exposed in an app
which wants to keep it hidden.
On Tue, Nov 13, 2018 at 4:30 PM Magic Len (Ron Li) ***@***.***> wrote:
In your case, the array can be easily used in JavaScript with a for loop
or the forEach function.
let arr = [['some', 'this'], ['writer', 'joke']];arr.forEach(function(e) {
sb.replaceAll(e[0], e[1]);
});
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADCMuv2WUGSJNoO15M985FG0tbgFx02Bks5uuljfgaJpZM4Ya6hS>
.
It would be useful if the ReplaceAll method could accept an array.
As an example, I altered the ReplaceAll code to produce this:
// from javascript call as: sb.replaceFromArray()
napi_value ReplaceFromArray(napi_env env, napi_callback_info info){
size_t argsLength = 1;
napi_value args[2]; // will hold parameters from a 2d array during a for loop
napi_value me;
int n;
}
I also note that the main section from replaceAll is mostly identical to that inside the ReplacePattern function. There is a potential therefore for refactoring, by which this section could be called from a separate function.
A more sophisticated implementation of ReplaceFromArray() would pass the array as a parameter, although embedding a hard-coded array has the advantage of "hiding" the array contents, while still suiting many possible applications.
The text was updated successfully, but these errors were encountered: