Skip to content

Replace from an array of strings #2

@xthdraft

Description

@xthdraft

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions