Y-lib
Loadrunner libraries
Functions
y_string.c File Reference

Y-lib string function library. More...

#include "y_core.c"

Go to the source code of this file.

Functions

void y_copy_param (char *source_param, char *dest_param)
 Copy a parameter to a new name. This is a semi-efficiënt parameter copy using lr_eval_string_ext(), with appropriate freeing of memory. More...
 
void y_uppercase_parameter (const char *param_name)
 Convert the content of a parameter to UPPERCASE. More...
 
void y_substr (const char *original_parameter, const char *result_parameter, const char *left, const char *right)
 Save a substring of a parameter into a new parameter. Search for a specific substring inside a parameter using left and right boundaries and save that into a new parameter. More...
 
void y_left (const char *original_parameter, const char *search, const char *result_parameter)
 Split a string into 2 parts using the search string. Save the left part into the result parameter. More...
 
void y_right (const char *original_parameter, const char *search, const char *result_parameter)
 Split a string into 2 parts using the search string. Save the right part into the result parameter. More...
 
void y_last_right (const char *original_parameter, const char *search, const char *result_parameter)
 Split a string into 2 parts using the search string. Save the rightmost part into the result parameter. This is almost the same as y_right(), but doesn't stop at the first match - instead, it uses the last match. It's pretty much the difference between 'greedy' and 'not greedy' in a regular expression.. More...
 
void y_split_str (const char *original, const char *separator, char *left, char *right)
 Split a string into 2 parts based on a search string. More...
 
void y_split (const char *originalParameter, const char *separator, const char *leftParameter, const char *rightParameter)
 Split a parameter in two based on a seperating string. If the seperator is not found in the original parameter the original parameter will be stored in it's entirety in the left hand parameter. More...
 
void y_chop (const char *parameter)
 Remove leading and trailing whitespace from a parameter. More...
 
void y_replace (const char *parameter, const char *search, const char *replace)
 Search and replace inside a parameter. This replaces the content of the originally passed-in parameter with the new content when done. More...
 
void y_remove_string_from_parameter (const char *paramName, const char *removeMe)
 Remove all occurrences of a specified text from a parameter. More...
 
void y_param_unique (char *param)
 Create a unique parameter. More...
 
void y_random_string_buffer_core (const char *parameter, int minimumLength, int maximumLength, int minWordLength, int maxWordLength, char *characterSet)
 Generates a random string with (pseudo) words created from a given string of characters. More...
 
void y_random_string_buffer (const char *parameter, int minimumLength, int maximumLength)
 Returns a random string with (pseudo) words created from a given string of characters. More...
 
void y_random_number_buffer (const char *parameter, int minimumLength, int maximumLength)
 Returns a random string of numbers with a given minimum and maximum length. More...
 
void y_random_string_buffer_curses (const char *parameter, int minimumLength, int maximumLength)
 Returns a string containing only the "shift-1...shift 9 characters (on a US-keyboard). More...
 
void y_random_string_buffer_hex (const char *parameter, int minimumLength, int maximumLength)
 Generates a random string with a hexadecimal number, of a given minimum and maximum length. More...
 
char * y_get_cleansed_parameter (const char *param_name, char replacement)
 Get the content of a parameter without embedded null bytes (\0 characters) from the named parameter, if any. In some cases we want to fetch the content of a parameter but the parameter contains embedded NULL characters which make further processing harder. This will fetch a parameter but "cleanse" it from such contamination, leaving the rest of the data unaltered before returning it. More...
 
void y_cleanse_parameter_ext (const char *param_name, char replacement)
 Clean a parameter by replacing any embedded NULL (null) characters with a replacement character. More...
 
void y_cleanse_parameter (const char *param_name)
 Clean a parameter by replacing any embedded NULL (null) characters with a space. This is identical to y_cleanse_parameter_ext() with " " (a single space) selected as the replacement character. More...
 

Detailed Description

Y-lib string function library.

Contains low level string and memory manipulation functions, insofar not provided by the C standard. The philosophy of ylib is that the script engineer should not be required to worry about C-strings and C-like memory manipulation when parameters will suffice. Most string manipulation functions in the y-lib library take loadrunner parameters as arguments and place their output in one or more of these parameters. This usually makes it easy to correlate a value (capturing it in a parameter), process it, then pass it on to the next request (again as a parameter).

Definition in file y_string.c.

Function Documentation

void y_chop ( const char *  parameter)

Remove leading and trailing whitespace from a parameter.

This does not support unicode, so it may not catch everything. Supported whitespace is: " "(=space) "\r"(=carrige return) "\n"(=line feed) "\t"(=tab) The result is stored in the original parameter.

Parameters
[in]parameterThe parameter to chop.

Example:

1 lr_save_string(" WackoYackoDot ", "Test");
2 lr_message(lr_eval_string("Original: >{Test}<\n")); // {Test}= " WackoYackoDot "
3 y_chop("Test");
4 lr_message(lr_eval_string("Original: >{Test}<\n")); // {Test}= "WackoYackoDot"
Author
: Floris Kraak

Definition at line 455 of file y_string.c.

void y_cleanse_parameter ( const char *  param_name)

Clean a parameter by replacing any embedded NULL (null) characters with a space. This is identical to y_cleanse_parameter_ext() with " " (a single space) selected as the replacement character.

Parameters
[in]param_nameThe parameter to cleanse of nulls.
Warning
Since this changes existing parameters be careful what types of parameters you use this on.

Example:

1 {
2  char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' };
3  lr_save_var(buffer, 11, 0, "broken");
4  y_cleanse_parameter("broken"); // will save " b ro ken " into the "broken" parameter.
5 }

Definition at line 957 of file y_string.c.

void y_cleanse_parameter_ext ( const char *  param_name,
char  replacement 
)

Clean a parameter by replacing any embedded NULL (null) characters with a replacement character.

This would normally only happen if you have used to web_reg_save_param() and the result contains one or more null-character(s). Any such characters are replaced with replacement_char and the result is stored in the original parameter. When no null-character is found, the result is unaltered.

Warning
Since the return value is allocated with malloc(), it will need to be freed using free() at some point.
Parameters
[in]param_nameThe parameter to cleanse of nulls.
[in]replacementA character that replaces any embedded nulls found.
Warning
Since this changes existing parameters be careful what types of parameters you use this on.

Example:

1 {
2  char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' };
3  lr_save_var(buffer, 11, 0, "broken");
4  y_cleanse_parameter_ext("broken", '!'); // will save "!b!ro!ken!!" into the "broken" parameter.
5 }

Definition at line 926 of file y_string.c.

void y_copy_param ( char *  source_param,
char *  dest_param 
)

Copy a parameter to a new name. This is a semi-efficiënt parameter copy using lr_eval_string_ext(), with appropriate freeing of memory.

Parameters
[in]source_paramThe parameter to copy.
[in]dest_paramThe name of the parameter to copy the first parameter to.

Example:

1 lr_save_string("text", "param_a");
2 y_copy_param("param_a", "param_b"); // Results in an exact copy of the content of param_a being stored in param_b.
3 lr_log_message(lr_eval_string("param_b: {param_b}")); // Prints "param_b: text".
Author
Floris Kraak

Definition at line 86 of file y_string.c.

char* y_get_cleansed_parameter ( const char *  param_name,
char  replacement 
)

Get the content of a parameter without embedded null bytes (\0 characters) from the named parameter, if any. In some cases we want to fetch the content of a parameter but the parameter contains embedded NULL characters which make further processing harder. This will fetch a parameter but "cleanse" it from such contamination, leaving the rest of the data unaltered before returning it.

Warning
The return value of this function needs to be freed using lr_eval_string_ext_free().
Parameters
[in]param_nameThe parameter to cleanse of nulls.
[in]replacementA character that replaces any embedded nulls found.
Returns
The resulting parameter content.

Example:

1 {
2  char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' };
3  char *tmp;
4  lr_save_var(buffer, 11, 0, "broken");
5  tmp = y_get_cleansed_parameter("broken", '!');
6  lr_log_message("Result: %s", tmp); // Prints "Result: !b!ro!ken!!".
7  free(tmp);
8 }

Definition at line 868 of file y_string.c.

void y_last_right ( const char *  original_parameter,
const char *  search,
const char *  result_parameter 
)

Split a string into 2 parts using the search string. Save the rightmost part into the result parameter. This is almost the same as y_right(), but doesn't stop at the first match - instead, it uses the last match. It's pretty much the difference between 'greedy' and 'not greedy' in a regular expression..

Parameters
[in]original_parameterThe parameter to search.
[in]searchThe text preceding the text we're looking for.
[in]result_parameterThe name of the parameter to store the result in.

Example:

1 lr_save_string("AstrixObelixIdefix", "Test");
2 lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix
3 y_right( "Test", "Obelix", "Test4" );
4 lr_message(lr_eval_string("New Param: {Test4}\n")); // {Test4}=Idefix
Author
Floris Kraak

Definition at line 290 of file y_string.c.

void y_left ( const char *  original_parameter,
const char *  search,
const char *  result_parameter 
)

Split a string into 2 parts using the search string. Save the left part into the result parameter.

Parameters
[in]original_parameterThe parameter to search.
[in]searchThe text after the text we're looking for.
[in]result_parameterThe name of the parameter to store the result in.

Example:

1 lr_save_string("AstrixObelixIdefix", "Test");
2 lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix
3 y_left( "Test", "Obelix", "Test2" );
4 lr_message(lr_eval_string("New Param: {Test2}\n")); // {Test2}=Astrix
Author
Floris Kraak

Definition at line 188 of file y_string.c.

void y_param_unique ( char *  param)

Create a unique parameter.

Parameters
paramThe name of a parameter to store the resulting string in. Length is always 22 (base64) characters.
Returns
void
Author
Floris Kraak & André Luyer

Example:

1 y_param_unique("test");

Definition at line 636 of file y_string.c.

void y_random_number_buffer ( const char *  parameter,
int  minimumLength,
int  maximumLength 
)

Returns a random string of numbers with a given minimum and maximum length.

This function generates a string of numbers with a given minimum and maximum length.

Parameters
[out]parameterName of the LR-parameter in which the result is stored
[in]minimumLengthMinumum length of the string
[in]maximumLengthMaximum length of the string
Returns
void
Author
Raymond de Jongh
See also
y_random_string_buffer_core

Definition at line 801 of file y_string.c.

void y_random_string_buffer ( const char *  parameter,
int  minimumLength,
int  maximumLength 
)

Returns a random string with (pseudo) words created from a given string of characters.

This function uses a given set of characters to create words, separated by spaces. The words are minimal 3 characters long, and maximum 8 characters long. Should you need other word lenghts, use y_random_number_buffer_core(). The total length of the line is minimal minimumLength and maximum maximumLength long.

Parameters
[out]parameterName of the LR-parameter in which the result is stored
[in]minimumLengthMinumum length of the string
[in]maximumLengthMaximum length of the string
Returns
void
Author
Raymond de Jongh
See also
y_random_string_buffer_core

Definition at line 784 of file y_string.c.

void y_random_string_buffer_core ( const char *  parameter,
int  minimumLength,
int  maximumLength,
int  minWordLength,
int  maxWordLength,
char *  characterSet 
)

Generates a random string with (pseudo) words created from a given string of characters.

This function uses a given set of characters to create words, separated by spaces. The words are minimal minWordLength characters long, and maximum minWordLength characters. The total length of the line is minimal minimumLength and maimum maximumLength long.

Example:

1 // Generates a string of minimal 3 and max 20 characters,
2 // with words of minimal 1 and maximal 3 charactes.
3 // Chooses only characters a, c, d or d.
4 y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd");
5 
6 // Generates some sort of mock morse-code of exactly 30 characters.
7 // with words of minimal 1 and maximal 3 charactes.
8 // Chooses only characters a, c, d or d.
9 y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd"); // could result in "ccc db dac c"
Parameters
[out]parameterName of the LR-parameter in which the result is stored
[in]minimumLengthMinumum length of the string
[in]maximumLengthMaximum length of the string
[in]minWordLengthMinimum length of the words within the string
[in]maxWordLengthMinimum length of the words within the string
[in]characterSetThe string is build from this string of characters
Returns
void
Author
Floris Kraak / Raymond de Jongh
See also
y_random_number_buffer
y_random_string_buffer_curses
y_random_string_buffer
y_random_string_buffer_hex

Definition at line 678 of file y_string.c.

void y_random_string_buffer_curses ( const char *  parameter,
int  minimumLength,
int  maximumLength 
)

Returns a string containing only the "shift-1...shift 9 characters (on a US-keyboard).

This function generates a string of non-alfa-characters with a given minimum and maximum length.

Parameters
[out]parameterName of the LR-parameter in which the result is stored
[in]minimumLengthMinumum length of the string
[in]maximumLengthMaximum length of the string
Returns
void
Author
Raymond de Jongh
See also
y_random_string_buffer_core

Definition at line 818 of file y_string.c.

void y_random_string_buffer_hex ( const char *  parameter,
int  minimumLength,
int  maximumLength 
)

Generates a random string with a hexadecimal number, of a given minimum and maximum length.

This function generates a string with a hexadecimal number.

Should you need other word lenghts, use y_random_number_buffer_core(). The total length of the line is minimal minimumLength and maimum maximumLength long.

Parameters
[out]parameterName of the LR-parameter in which the result is stored
[in]minimumLengthMinumum length of the string
[in]maximumLengthMaximum length of the string
Returns
void
Author
Raymond de Jongh
See also
y_random_string_buffer_core

Definition at line 838 of file y_string.c.

void y_remove_string_from_parameter ( const char *  paramName,
const char *  removeMe 
)

Remove all occurrences of a specified text from a parameter.

This is a lighter weight alternative to the y_replace() function in cases where just want to remove text, rather than replace it with something else. Stores the result in the original parameter.

Parameters
[in]paramNameThe parameter to search.
[in]removeMeThe text to remove.

Example:

1 lr_save_string("test123", "par1");
2 y_remove_string_from_parameter("par1", "1"); // {par1} now has the value test23

Definition at line 594 of file y_string.c.

void y_replace ( const char *  parameter,
const char *  search,
const char *  replace 
)

Search and replace inside a parameter. This replaces the content of the originally passed-in parameter with the new content when done.

Note
This one has a built-in search/y_replace limit. After 1000 replacements it will stop. If 1000 replacements in a single parameter does not suffice consider using other methods.
Parameters
[in]parameterThe parameter to search.
[in]searchWhat to search for.
[in]replaceWhat to replace it with.

Example:

1 lr_save_string("test123", "par1");
2 y_replace("par1", "1", "ing1"); // {par1} now has the value testing123

Definition at line 508 of file y_string.c.

void y_right ( const char *  original_parameter,
const char *  search,
const char *  result_parameter 
)

Split a string into 2 parts using the search string. Save the right part into the result parameter.

Parameters
[in]original_parameterThe parameter to search.
[in]searchThe text preceding the text we're looking for.
[in]result_parameterThe name of the parameter to store the result in.

Example:

1 lr_save_string("AstrixObelixIdefix", "Test");
2 lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix
3 y_right( "Test", "Obelix", "Test4" );
4 lr_message(lr_eval_string("New Param: {Test4}\n")); // {Test4}=Idefix
Author
Floris Kraak

Definition at line 240 of file y_string.c.

void y_split ( const char *  originalParameter,
const char *  separator,
const char *  leftParameter,
const char *  rightParameter 
)

Split a parameter in two based on a seperating string. If the seperator is not found in the original parameter the original parameter will be stored in it's entirety in the left hand parameter.

Parameters
[in]originalParameterThe parameter to search.
[in]separatorThe string to use as a seperation marker between the two parts.
[in]leftParameterThe parameter that will hold the left hand side of the split result.
[in]rightParameterThe parameter that will hold the right hand side of the split result.

Example:

1 lr_save_string("WackoYackoDotWarner", "Test");
2 lr_message(lr_eval_string("Original: {Test}\n")); // {Test} = WackoYackoDotWarner
3 y_split("Test", "Yacko", "Left", "Right"); // Use "Yacko" as the separator
4 lr_message(lr_eval_string("Original: {Test}\n")); // {Test} = WackoYackoDotWarner
5 lr_message(lr_eval_string("Left : {Left}\n")); // {Left} = Wacko
6 lr_message(lr_eval_string("Right : {Right}\n")); // {Right} = DotWarner
Author
Floris Kraak

Definition at line 389 of file y_string.c.

void y_split_str ( const char *  original,
const char *  separator,
char *  left,
char *  right 
)

Split a string into 2 parts based on a search string.

Warning
Unlike the others this one does not use parameter, but raw char pointers instead. This mostly to accomodate y_array_split(). For the parameter version use y_split().
Parameters
[in]originalThe string to search.
[in]separatorThe string to use as a seperation marker between the two parts.
[in]leftA preallocated char* buffer to hold the left hand side of the result.
[in]rightA preallocated char* buffer to hold the right hand side of the result.
Author
Floris Kraak

Definition at line 341 of file y_string.c.

void y_substr ( const char *  original_parameter,
const char *  result_parameter,
const char *  left,
const char *  right 
)

Save a substring of a parameter into a new parameter. Search for a specific substring inside a parameter using left and right boundaries and save that into a new parameter.

Parameters
[in]original_parameterThe parameter to search.
[in]result_parameterThe name of the parameter to store the result in.
[in]leftThe left boundary - the text immediately preceding the substring in question.
[in]rightThe right boundary.

Example:

1 char* str = "LorumIpsumLipsum";
2 lr_save_string(str, "param");
3 y_substr("param", "param", "Lorum", "Lipsum");
4 lr_log_message(lr_eval_string("{param}")); // Prints "Ipsum".
Author
André Luyer, Marcel Jepma & Floris Kraak

Definition at line 144 of file y_string.c.

void y_uppercase_parameter ( const char *  param_name)

Convert the content of a parameter to UPPERCASE.

This will replace the content of the paramenter named in 'param_name' with the uppercased version. Does not affect non-alphabetic characters.

Parameters
[in]param_nameThe parameter to convert to uppercase.

Example:

1 lr_save_string("aBcDeFgHiJ &*45#$@#)!({}", "Test");
2 lr_message(lr_eval_string("Original: {Test}\n"));
3 y_uppercase_parameter("Test");
4 lr_message(lr_eval_string("Altered: {Test}\n")); // prints "Altered: ABCDEFGHIJ &*45#$@#)!({}".
Author
Floris Kraak

Definition at line 114 of file y_string.c.