File: ManageString.cmake

package info (click to toggle)
ibus-table-chinese 1.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 29,812 kB
  • sloc: sh: 106; makefile: 11
file content (325 lines) | stat: -rw-r--r-- 11,147 bytes parent folder | download | duplicates (3)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# - Collection of String utility macros.
#
# Included by:
#   ManageVarible
#
# Defines the following macros:
#   STRING_TRIM(var str [NOUNQUOTE])
#   - Trim a string by removing the leading and trailing spaces,
#     just like STRING(STRIP ...) in CMake 2.6 and later.
#     This macro is needed as CMake 2.4 does not support STRING(STRIP ..)
#     This macro also remove quote and double quote marks around the string,
#     unless NOUNQUOTE is defined.
#     * Parameters:
#       + var: A variable that stores the result.
#       + str: A string.
#       + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#
#   STRING_ESCAPE_SEMICOLON(var str)
#   - Escape the semicolon
#     * Parameters:
#       + var: A variable that stores the result.
#       + str: A string.
#
#   STRING_UNQUOTE(var str)
#   - Remove double quote marks and quote marks around a string.
#     If the string is not quoted, then content of str is copied to var
#     * Parameters:
#       + var: A variable that stores the result.
#       + str: A string.
#
#   STRING_JOIN(var delimiter str_list [str...])
#   - Concatenate strings, with delimiter inserted between strings.
#     * Parameters:
#       + var: A variable that stores the result.
#       + str_list: A list of string.
#       + str: (Optional) more string to be join.
#
#   STRING_SPLIT(var delimiter str [NOESCAPE_SEMICOLON])
#   - Split a string into a list using a delimiter, which can be in 1 or more
#     characters long.
#     * Parameters:
#       + var: A variable that stores the result.
#       + delimiter: To separate a string.
#       + str: A string.
#       + NOESCAPE_SEMICOLON: (Optional) Do not escape semicolons.
#

IF(NOT DEFINED _MANAGE_STRING_CMAKE_)
    SET(_MANAGE_STRING_CMAKE_ "DEFINED")

    # Return (index of lefttmost non match character)
    # Return _strLen if all characters matches regex
    FUNCTION(STRING_LEFTMOST_NOTMATCH_INDEX var str regex)
	STRING(LENGTH "${str}" _strLen)
	SET(_index 0)
	SET(_ret ${_strLen})
	WHILE(_index LESS _strLen)
	    STRING(SUBSTRING "${str}" ${_index} 1 _strCursor)
	    #MESSAGE("***STRING_UNQUOTE: _i=${_index} _strCursor=${_strCursor}")
	    IF(NOT "${_strCursor}" MATCHES "${regex}")
		SET(_ret ${_index})
		SET(_index ${_strLen})
	    ENDIF(NOT "${_strCursor}" MATCHES "${regex}")

	    MATH(EXPR _index ${_index}+1)
	ENDWHILE(_index LESS _strLen)
	SET(${var} ${_ret} PARENT_SCOPE)
    ENDFUNCTION(STRING_LEFTMOST_NOTMATCH_INDEX var str)

    # Return (index of rightmost non match character) +1
    # Return 0 if all characters matches regex
    #
    FUNCTION(STRING_RIGHTMOST_NOTMATCH_INDEX var str regex)
	STRING(LENGTH "${str}" _strLen)
	MATH(EXPR _index ${_strLen})
	SET(_ret 0)
	WHILE(_index GREATER 0)
	    MATH(EXPR _index_1 ${_index}-1)
	    STRING(SUBSTRING "${str}" ${_index_1} 1 _strCursor)
	    #MESSAGE("***STRING_UNQUOTE: _i=${_index} _strCursor=${_strCursor}")

	    IF(NOT "${_strCursor}" MATCHES "${regex}")
		SET(_ret ${_index})
		SET(_index 0)
	    ENDIF(NOT "${_strCursor}" MATCHES "${regex}")
	    MATH(EXPR _index ${_index}-1)
	ENDWHILE(_index GREATER 0)
	SET(${var} ${_ret} PARENT_SCOPE)
    ENDFUNCTION(STRING_RIGHTMOST_NOTMATCH_INDEX var str)

    FUNCTION(STRING_TRIM var str)
	#_STRING_ESCAPE(_ret "${str}" ${ARGN})
	STRING_LEFTMOST_NOTMATCH_INDEX(_leftIndex "${str}" "[ \t\n\r]")
	STRING_RIGHTMOST_NOTMATCH_INDEX(_rightIndex "${str}" "[ \t\n\r]")
	#MESSAGE("_left=${_leftIndex} _rightIndex=${_rightIndex} str=|${str}|")
	MATH(EXPR _subLen ${_rightIndex}-${_leftIndex})
	SET(_NOUNQUOTE 0)
	FOREACH( _arg ${ARGN})
	    IF(_arg STREQUAL "NOUNQUOTE")
		SET(_NOUNQUOTE 1)
	    ENDIF(_arg STREQUAL "NOUNQUOTE")
	ENDFOREACH( _arg ${ARGN})

	IF(_subLen GREATER 0)
	    STRING(SUBSTRING "${str}" ${_leftIndex} ${_subLen} _ret)
	    # IF _subLen > 1
	    #   IF UNQUOTE; then unquote
	    # Otherwise don't touch
	    IF (_subLen GREATER 1)
		IF(NOT _NOUNQUOTE)
		    STRING_UNQUOTE(_ret "${_ret}")
		ENDIF(NOT _NOUNQUOTE)
	    ENDIF (_subLen GREATER 1)
	ELSE(_subLen GREATER 0)
	    SET(_ret "")
	ENDIF(_subLen GREATER 0)
	SET(${var} "${_ret}" PARENT_SCOPE)

	# Unencoding
	#_STRING_UNESCAPE(${var} "${_ret}" ${ARGN})
    ENDFUNCTION(STRING_TRIM var str)

    MACRO(STRING_ESCAPE_SEMICOLON var str)
	STRING(REGEX REPLACE ";" "\\\\;" ${var} "${str}")
    ENDMACRO(STRING_ESCAPE_SEMICOLON var str)

    # Internal macro
    # Nested Variable cannot be escaped here, as variable is already substituted
    # at the time it passes to this macro.
    MACRO(_STRING_ESCAPE var str)
	# ';' and '\' are tricky, need to be encoded.
	# '#' => '#H'
	# '\' => '#B'
	# ';' => '#S'
	SET(_NOESCAPE_SEMICOLON "")
	SET(_NOESCAPE_HASH "")

	FOREACH(_arg ${ARGN})
	    IF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
		SET(_NOESCAPE_SEMICOLON "NOESCAPE_SEMICOLON")
	    ELSEIF(${_arg} STREQUAL "NOESCAPE_HASH")
		SET(_NOESCAPE_HASH "NOESCAPE_HASH")
	    ENDIF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
	ENDFOREACH(_arg ${ARGN})

	IF(_NOESCAPE_HASH STREQUAL "")
	    STRING(REGEX REPLACE "#" "#H" _ret "${str}")
	ELSE(_NOESCAPE_HASH STREQUAL "")
	    SET(_ret "${str}")
	ENDIF(_NOESCAPE_HASH STREQUAL "")

	STRING(REGEX REPLACE "\\\\" "#B" _ret "${_ret}")
	IF(_NOESCAPE_SEMICOLON STREQUAL "")
	    STRING(REGEX REPLACE ";" "#S" _ret "${_ret}")
	ENDIF(_NOESCAPE_SEMICOLON STREQUAL "")
	#MESSAGE("_STRING_ESCAPE:_ret=${_ret}")
	SET(${var} "${_ret}")
    ENDMACRO(_STRING_ESCAPE var str)

    MACRO(_STRING_UNESCAPE var str)
	# '#B' => '\'
	# '#H' => '#'
	# '#D' => '$'
	# '#S' => ';'
	SET(_ESCAPE_VARIABLE "")
	SET(_NOESCAPE_SEMICOLON "")
	SET(_ret "${str}")
	FOREACH(_arg ${ARGN})
	    IF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
		SET(_NOESCAPE_SEMICOLON "NOESCAPE_SEMICOLON")
	    ELSEIF(${_arg} STREQUAL "ESCAPE_VARIABLE")
		SET(_ESCAPE_VARIABLE "ESCAPE_VARIABLE")
		STRING(REGEX REPLACE "#D" "$" _ret "${_ret}")
	    ENDIF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
	ENDFOREACH(_arg ${ARGN})
	#MESSAGE("###_STRING_UNESCAPE: var=${var} _ret=${_ret} _NOESCAPE_SEMICOLON=${_NOESCAPE_SEMICOLON} ESCAPE_VARIABLE=${_ESCAPE_VARIABLE}")

	STRING(REGEX REPLACE "#B" "\\\\" _ret "${_ret}")
	IF("${_NOESCAPE_SEMICOLON}" STREQUAL "")
	    # ESCAPE_SEMICOLON
	    STRING(REGEX REPLACE "#S" "\\\\;" _ret "${_ret}")
	ELSE("${_NOESCAPE_SEMICOLON}" STREQUAL "")
	    # Don't ESCAPE_SEMICOLON
	    STRING(REGEX REPLACE "#S" ";" _ret "${_ret}")
	ENDIF("${_NOESCAPE_SEMICOLON}" STREQUAL "")

	IF(NOT _ESCAPE_VARIABLE STREQUAL "")
	    # '#D' => '$'
	    STRING(REGEX REPLACE "#D" "$" _ret "${_ret}")
	ENDIF(NOT _ESCAPE_VARIABLE STREQUAL "")
	STRING(REGEX REPLACE "#H" "#" _ret "${_ret}")
	SET(${var} "${_ret}")
	#MESSAGE("*** _STRING_UNESCAPE: ${var}=${${var}}")
    ENDMACRO(_STRING_UNESCAPE var str)

    MACRO(STRING_UNQUOTE var str)
	SET(_ret "${str}")
	STRING(LENGTH "${str}" _strLen)

	# IF _strLen > 1
	#   IF lCh and rCh are both "\""
	#      Remove _lCh and _rCh
	#   ELSEIF lCh and rCh are both "'"
	#      Remove _lCh and _rCh
	# Otherwise don't touch
	IF(_strLen GREATER 1)
	    STRING(SUBSTRING "${str}" 0 1 _lCh)
	    MATH(EXPR _strLen_1 ${_strLen}-1)
	    MATH(EXPR _strLen_2 ${_strLen_1}-1)
	    STRING(SUBSTRING "${str}" ${_strLen_1} 1 _rCh)
	    #MESSAGE("_lCh=${_lCh} _rCh=${_rCh} _ret=|${_ret}|")
	    IF("${_lCh}" STREQUAL "\"" AND "${_rCh}" STREQUAL "\"")
		STRING(SUBSTRING "${_ret}" 1 ${_strLen_2} _ret)
	    ELSEIF("${_lCh}" STREQUAL "'" AND "${_rCh}" STREQUAL "'")
		STRING(SUBSTRING "${_ret}" 1 ${_strLen_2} _ret)
	    ENDIF("${_lCh}" STREQUAL "\"" AND "${_rCh}" STREQUAL "\"")
	ENDIF (_strLen GREATER 1)
	SET(${var} "${_ret}")
    ENDMACRO(STRING_UNQUOTE var str)

    MACRO(STRING_JOIN var delimiter str_list)
	SET(_ret "")
	FOREACH(_str ${str_list})
	    IF(_ret STREQUAL "")
		SET(_ret "${_str}")
	    ELSE(_ret STREQUAL "")
		SET(_ret "${_ret}${delimiter}${_str}")
	    ENDIF(_ret STREQUAL "")
	ENDFOREACH(_str ${str_list})

	FOREACH(_str ${ARGN})
	    IF(_ret STREQUAL "")
		SET(_ret "${_str}")
	    ELSE(_ret STREQUAL "")
		SET(_ret "${_ret}${delimiter}${_str}")
	    ENDIF(_ret STREQUAL "")
	ENDFOREACH(_str ${ARGN})
	SET(${var} "${_ret}")
    ENDMACRO(STRING_JOIN var delimiter str_list)

    FUNCTION(STRING_FIND var str search_str)
	STRING(LENGTH "${str}" _str_len)
	STRING(LENGTH "${search_str}" _search_len)
	MATH(EXPR _str_end ${_str_len}-${_search_len}+1)

	SET(_index 0)
	SET(_str_remain "")
	SET(_result -1)
	WHILE(_index LESS _str_end)
	    STRING(SUBSTRING "${str}" ${_index} ${_search_len} _str_window)
	    IF("${_str_window}" STREQUAL "${search_str}")
		SET(_result ${_index})
		BREAK()
	    ELSE("${_str_window}" STREQUAL "${search_str}")
		MATH(EXPR _index ${_index}+1)
	    ENDIF("${_str_window}" STREQUAL "${search_str}")
	ENDWHILE(_index LESS _str_end)
	SET(${var} ${_result} PARENT_SCOPE)
    ENDFUNCTION(STRING_FIND var str search)

    FUNCTION(STRING_SPLIT_2 var str_remain delimiter str)
	STRING_FIND(_index "${str}" "${delimiter}")
	IF(_index EQUAL -1)
	    SET(${var} "${str}" PARENT_SCOPE)
	    SET(${str_remain} "" PARENT_SCOPE)
	ELSE(_index EQUAL -1)
	    STRING(SUBSTRING "${str}" 0 ${_index} _var)
	    SET(${var} "${_var}" PARENT_SCOPE)

	    STRING(LENGTH "${str}" _str_len)
	    STRING(LENGTH "${delimiter}" _delimiter_len)
	    MATH(EXPR _str_2_start ${_index}+${_delimiter_len})
	    MATH(EXPR _str_2_len ${_str_len}-${_index}-${_delimiter_len})
	    STRING(SUBSTRING "${str}" ${_str_2_start} ${_str_2_len} _str_remain)
	    SET(${str_remain} "${_str_remain}" PARENT_SCOPE)
	ENDIF(_index EQUAL -1)
    ENDFUNCTION(STRING_SPLIT_2 var str_remain delimiter str)

    MACRO(STRING_SPLIT var delimiter str)
	#MESSAGE("***STRING_SPLIT: var=${var} str=${str}")
	SET(_max_tokens "")
	SET(_NOESCAPE_SEMICOLON "")
	SET(_ESCAPE_VARIABLE "")
	FOREACH(_arg ${ARGN})
	    IF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
		SET(_NOESCAPE_SEMICOLON "NOESCAPE_SEMICOLON")
	    ELSEIF(${_arg} STREQUAL "ESCAPE_VARIABLE")
		SET(_ESCAPE_VARIABLE "ESCAPE_VARIABLE")
	    ELSE(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
		SET(_max_tokens ${_arg})
	    ENDIF(${_arg} STREQUAL "NOESCAPE_SEMICOLON")
	ENDFOREACH(_arg ${ARGN})

	IF(NOT _max_tokens)
	    SET(_max_tokens -1)
	ENDIF(NOT _max_tokens)

	_STRING_ESCAPE(_str "${str}" ${_NOESCAPE_SEMICOLON} ${_ESCAPE_VARIABLE})
	_STRING_ESCAPE(_delimiter "${delimiter}" ${_NOESCAPE_SEMICOLON} ${_ESCAPE_VARIABLE})
	SET(_str_list "")
	SET(_token_count 1)

	WHILE(NOT _token_count EQUAL _max_tokens)
	    STRING_SPLIT_2(_token _str "${_delimiter}" "${_str}")
	    #MESSAGE("_token_count=${_token_count} _max_tokens=${_max_tokens} _token=${_token} _str=${_str}")
	    MATH(EXPR _token_count ${_token_count}+1)
	    LIST(APPEND _str_list "${_token}")
	    IF("${_str}" STREQUAL "")
		## No more tokens
		BREAK()
	    ENDIF("${_str}" STREQUAL "")
	ENDWHILE(NOT _token_count EQUAL _max_tokens)

	IF(NOT "x${_str}" STREQUAL "x")
	    ## Append last part
	    LIST(APPEND _str_list "${_str}")
	ENDIF(NOT "x${_str}" STREQUAL "x")

	# Unencoding
	_STRING_UNESCAPE(${var} "${_str_list}" ${_NOESCAPE_SEMICOLON} ${_ESCAPE_VARIABLE})
	#MESSAGE("***STRING_SPLIT: tokens=${${var}}")
    ENDMACRO(STRING_SPLIT var delimiter str)

ENDIF(NOT DEFINED _MANAGE_STRING_CMAKE_)