summaryrefslogtreecommitdiffstats
path: root/tapset/string.stp
blob: 2f43aeccbbc1d89f9b342fdf2a5f2bde723d9aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** @addtogroup library
* The library tapset is a collection of standard functions.
* @{
*/

/** @addtogroup library
* @code function strlen:long(s:string) @endcode
* @param s string
* @return Returns the length of the string.
*/
function strlen:long(s:string) %{ /* pure */
        THIS->__retvalue = strlen(THIS->s);
%}

/** @addtogroup library
* @code function substr:string(str:string,start:long,length:long) @endcode
* @param str string
* @param start Starting position. 0 = start of the string
* @param length Length of string to return.
* @return Returns the substring.
*/
function substr:string(str:string,start:long, length:long) %{ /* pure */
	int length = THIS->length >= MAXSTRINGLEN ? MAXSTRINGLEN : THIS->length + 1;
	if (THIS->start >= 0 && length > 0 && THIS->start < strlen(THIS->str))
		strlcpy(THIS->__retvalue, THIS->str + THIS->start, length);
%}


/** @addtogroup library
* @code isinstr:long(s1:string,s2:string) @endcode
* @param s1 string
* @param s2 string
* @return Returns 1 if s2 is in s1. Otherwise 0.
*/
function isinstr:long(s1:string,s2:string) %{ /* pure */
	if (strstr(THIS->s1,THIS->s2) != NULL)
		THIS->__retvalue = 1;
	else
		THIS->__retvalue = 0;
%}

/*
 * text_str()
 *
 * Takes a string, and any ASCII characters that are not printable are
 * replaced by the corresponding escape sequence in the returned
 * string.
 */
function text_str:string(input:string)
%{ /* pure */
	_stp_text_str(THIS->__retvalue, THIS->input, 0, 0, 0);
%}

function text_strn:string(input:string, len:long, quoted:long)
%{ /* pure */
	_stp_text_str(THIS->__retvalue, THIS->input, THIS->len, THIS->quoted, 0);
%}

/*
 * tokenize - Given a string and a token delimiter,
 *            return the next token in the string
 * input  String to tokenize. If NULL, returns the next token in the
 *        string passed in the previous call to tokenize().
 * delim  Token delimiter. Note this is a string, but only the first
 *        character is used as the delimiter.
 */
function tokenize:string(input:string, delim:string)
%{ /* pure */
	static char str[MAXSTRINGLEN];
	static char *str_start;
	char *token = NULL;

	if (THIS->input[0]) {
		strncpy(str, THIS->input, MAXSTRINGLEN);
		str_start = &str[0];
	}
	token = strsep(&str_start, THIS->delim);
	if (token)
		strncpy (THIS->__retvalue, token, MAXSTRINGLEN);
%}

/*
 * strtol - Convert a string to a long
 *   str   String to convert
 *   base  The base to use
 */
function strtol:long(str:string, base:long)
%{ /* pure */
	THIS->__retvalue = simple_strtol(THIS->str, NULL, THIS->base);
%}

/** @} */