Varnish Cache Plus

Strings (str)

Description

vmod_str implements various helper function to manipulate strings.

API

INT len(STRING S)

Returns the number of ascii characters in S, or -1 if S is null.

BOOL startswith(STRING S1, STRING S2)

Returns true if S1 starts with S2.

BOOL endswith(STRING S1, STRING S2)

Returns true if S1 ends with S2.

BOOL contains(STRING S1, STRING S2)

Returns true if S1 contains S2.

STRING substr(STRING S, INT N, INT OFFSET = 0)

Returns a substring that is at most abs(N) characters long, from the character at the OFFSET position (negative OFFSET counts from the back of the string). If N is positive, the substring is extracted right of OFFSET, left otherwise.

Example:

  • substr(“0123456789abcdef”, 2, 5) -> “56”, 2 characters from the 5th one
  • substr(“0123456789abcdef”, 2, -5) -> “be”, 2 characters from the 5th to last one
  • substr(“0123456789abcdef”, -2, 5) -> “45”, 2 characters to the 5th one
  • substr(“0123456789abcdef”, -2, -5) -> “ab”, 2 characters to the 5th to last one
  • substr(“0123456789abcdef”, 10, 20) -> “abcdef”, truncated because the end of the string was reached.

If S in NULL, NULL is returned.

STRING reverse(STRING S)

Reverse S.

STRING split(STRING S, INT N, STRING SEP = " \t")

Split S and return the N-th token. Characters in SEP are separators. A negative N indicate “from the end of the string”.

BOOL token_intersect(STRING S1, STRING S2, SEPARATORS = " ,")

Checks if the set tokens in one string intersects the set of tokens in the other string. In other words, it returns true if there is a token which exists in both strings.

If the SEPARATORS argument is not supplied, the space and comma characters are the separators. Empty tokens are ignored in both strings.