1 /* Copyright 2013 Akira Ohta (akohta001@gmail.com)
2 This file is part of ntch.
4 The ntch is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 The ntch is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with ntch. If not, see <http://www.gnu.org/licenses/>.
24 #include "utils/nt_std_t.h"
25 #include "utils/text.h"
28 wchar_t* nt_w_str_resize(wchar_t *old_src, size_t old_size, size_t new_size)
32 assert(old_size < new_size && new_size > 0);
34 wrk_buf = malloc(sizeof(wchar_t) * (new_size));
43 wmemcpy(wrk_buf, old_src, old_size);
44 wrk_buf[old_size] = L'\0';
49 BOOL nt_w_str_move(wchar_t *buf, size_t buf_len,
50 int move_index, int offset)
55 str_len = wcslen(buf);
57 if(0 > move_index || str_len <= move_index)
60 tmp = str_len + offset;
68 // Move the block to forward.
69 if(move_index + offset < 0)
72 for(i = move_index; i < str_len; i++){
73 buf[i+offset] = buf[i];
75 buf[str_len+offset] = L'\0';
76 }else{/* if(offset > 0) */
77 // Move the block to backward.
78 if(str_len + offset >= buf_len)
80 for(i = str_len - 1; i >= move_index; i--){
81 buf[i + offset] = buf[i];
83 buf[str_len + offset] = L'\0';
84 buf[move_index] = L'\0';
91 wchar_t *nt_w_trim(const wchar_t *source)
97 assert(source != NULL);
101 for(i=0; i<len; i++){
117 cptr = malloc(sizeof(wchar_t));
122 for(j = len - 1; j > i; j--){
139 cptr = malloc(sizeof(wchar_t)*(len + 1));
141 memcpy(cptr, source+i, len*sizeof(wchar_t));
147 wchar_t *nt_w_trim_quotes(const wchar_t *source)
149 assert(source != NULL);
150 int len = wcslen(source);
151 int i, j, status = 0;
156 for(i=0; i<len; i++){
184 cptr = malloc(sizeof(wchar_t));
189 for(j = len - 1; j > i; j--){
221 cptr = malloc(sizeof(wchar_t)*(len + 1));
223 memcpy(cptr, source+i, len*sizeof(wchar_t));
227 cptr = malloc(sizeof(wchar_t));
235 char *nt_trim2(const char *source, char *buf, size_t buf_len)
241 assert(source != NULL);
243 len = strlen(source);
246 cptr = calloc(len+1, sizeof(char));
254 for(i=0; i<len; i++){
273 for(j = len - 1; j > i; j--){
290 memcpy(cptr, source+i, len*sizeof(char));
295 wchar_t* nt_w_str_clone(const wchar_t* src)
298 cptr = malloc(sizeof(wchar_t) * (wcslen(src)+1));
305 char* nt_str_clone(const char* src)
308 cptr = malloc(strlen(src)+1);
315 char* nt_substr(const char* src, int start, int end)
321 assert(start <= end);
324 cptr = malloc(len + 1);
333 memcpy(cptr, src+start, len);
338 BOOL nt_strtok(const char *str, char delim,
339 const char** start, const char **end)
350 for(i = 0; i < len; i++){
353 *start = str + i + 1;
354 }else if(state == 1){