"Sputnik" help  
Sputnik Help
strcmp

Description

The strcmp function in Sputnik's preprocessor is designed for comparing two strings, <s1> and <s2>, to determine their lexicographical relationship. This function is particularly useful within conditional statements, such as #if directives, where string comparison is a common requirement. Developers can leverage strcmp to make decisions based on the relative order of strings.

Parameters

<s1> The first string to be compared.
<s2> The second string to be compared.
[<start>] (Optional) Specifies the starting position for the comparison. If provided, the comparison starts from this position in both strings. Default is 0.
[<len>] (Optional) Specifies the length of the substring to be considered in the comparison. If provided, only a substring of the strings is compared. Default is the length of the shorter string.

Return Value

Success: Returns 0.
Failure: Returns lower/higher than zero.

Remarks

The strcmp function in Sputnik's preprocessor offers a versatile tool for comparing strings, allowing developers to make decisions within conditional statements based on the lexicographical relationship between two strings. The function operates with flexibility, supporting optional parameters for specifying starting positions and substring lengths, enabling precise control over the comparison process.

It's important to note that strcmp is particularly valuable within the context of #if directives, where conditional code execution depends on the result of string comparisons. Developers can leverage this function to create dynamic and adaptable code that responds to the relative order of strings.

Related

StrCaseCmp

Example (#)

Example of usage:

#define foo "cat"

#if strcmp(foo, "cat") == 0
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true


#define foo "dog"

#if strcmp(foo, "cat") == 0
    say "Condition is true (cat)";
#elif strcmp(foo, "dog") == 0
    say "Condition is true (dog)";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true (dog)

 

Example (.)

Example of usage:

#macro foo(x)
    .if strcmp(x, "foo") == 0
        say "It is foo";
    .elif strcmp(x, "bar") == 0
        say "It is bar";
    .else
        say "It is unknown";
    .endif
#endm

foo("cat");
foo("foo");
foo("bar");

// PRINTS
// It is unknown
// It is foo
// It is bar

Another example:

#macro Test1(x, y)
    .eq PAX, x
    .app PAX, "dog"
    .if strcmp(PAX, y) == 0
        "Match"
    .else
        "No Match"
    .endif
#endm

say Test1("cat", "catdog");
// PRINTS
// Match

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org