aboutsummaryrefslogtreecommitdiff
path: root/src/bi_string.m4
blob: dadad8aa13b8174606bc3284b4aff8c1b67dbcbd (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This file is part of mailfromd.             -*- c -*-
   Copyright (C) 2006, 2007, 2008 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

MF_DEFUN(toupper, STRING, STRING string)
{
	size_t off;
	char *s = MF_COPY_STRING(off, string);
	char *p;
	for (p = s; *p; p++)
		*p = toupper(*p);
	MF_RETURN(off);
}
END

MF_DEFUN(tolower, STRING, STRING string)
{
	size_t off;
	char *s = MF_COPY_STRING(off, string);
	char *p;
	for (p = s; *p; p++)
		*p = tolower(*p);
	MF_RETURN(off);
}
END

MF_DEFUN(length, NUMBER, STRING string)
{
	MF_RETURN(strlen(string));
}
END

MF_DEFUN(substring, STRING, STRING string, NUMBER start, NUMBER end)
{
	size_t off;
	long len = strlen(string);
	char *s;
	
	if (end < 0)
		end = len - 1;
	if (end < start) {
		long t = end;
		end = start;
		start = t;
	}

	MF_ASSERT(start < len && end < len, mfe_range,
		  _("Argument out of range"));
	
	len = end - start + 1;
	s = MF_ALLOC_HEAP(off, len + 1);
	memcpy(s, string + start, len);
	s[len] = 0;
	
	MF_RETURN(off);
}
END

MF_DEFUN(substr, STRING, STRING string, NUMBER start, OPTIONAL, NUMBER nbytes)
{
	size_t off;
	long len = strlen(string);
	char *s;
	
	MF_ASSERT(start >= 0, mfe_range,
		  _("Argument out of range: start=%ld"), start);
	if (!MF_DEFINED(nbytes))
		nbytes = len - start;
	MF_ASSERT(nbytes >= 0, mfe_range, 
	          _("Argument out of range: start=%ld, len=%ld"), start, len);
	
	s = MF_ALLOC_HEAP(off, nbytes + 1);
	memcpy(s, string + start, nbytes);
	s[nbytes] = 0;
	
	MF_RETURN(off);
}
END

MF_DEFUN(index, NUMBER, STRING str, STRING sub)
{
	char *p = strstr(str, sub);
	MF_RETURN(p ? p - str : -1);
}
END

MF_DEFUN(rindex, NUMBER, STRING str, STRING sub)
{
	int slen, xlen, rc;
	char *p, *s, *x;
	char *temp;

	slen = strlen(str);
	xlen = strlen(sub);
	temp = MF_ALLOC_HEAP_TEMP(xlen + slen + 2);
	s = temp;
	x = s + slen + 1;
	
	/* Reverse str */                     
#define REV(v,s,l)                                         \
	l = strlen(s);                                     \
	v[l] = 0;                                          \
	for (p = v + l - 1; p >= v; p--, s++)              \
		*p = *s;

	REV(s,str,slen);
	REV(x,sub,xlen);

	p = strstr(s, x);
	if (p)
		rc = slen - (p - s + xlen);
	else
		rc = -1;
	
	MF_RETURN(rc);
}
END

MF_DEFUN(revstr, STRING, STRING str)
{
	int len = strlen(str);
	char *p;
	size_t off;
	char *s = MF_ALLOC_HEAP(off, len + 1);

	s[len] = 0;
	for (p = s + len - 1; p >= s; p--, str++)
		*p = *str;
	MF_RETURN(off);
}
END

MF_DEFUN(message_header_decode, STRING, STRING text, OPTIONAL, STRING charset)
{
	char *p;
	int rc = mu_rfc2047_decode (MF_OPTVAL(charset, "utf-8"), text, &p);
	MF_ASSERT(rc == 0,
		  mf_failure,
		  _("Error decoding string: %s"),
		  mu_strerror(rc));
	pushs(env, p);
	free(p);
}
END

MF_DEFUN(message_header_encode, STRING, STRING text, OPTIONAL,
	 STRING encoding, STRING charset)
{
	char *p;
	int rc = mu_rfc2047_encode (MF_OPTVAL(charset, "utf-8"),
				    MF_OPTVAL(encoding, "quoted-printable"),
				    text, &p);
	MF_ASSERT(rc == 0,
		  mf_failure,
		  _("Error encoding string: %s"),
		  mu_strerror(rc));
	pushs(env, p);
	free(p);
}
END

MF_DEFUN(unfold, STRING, STRING text)
{
	size_t off;
	char *s = MF_COPY_STRING(off, text);
	mu_string_unfold(s, NULL);
	MF_RETURN(off);
}
END

MF_INIT

Return to:

Send suggestions and report system problems to the System administrator.