forked from JDimproved/JDim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miscutil.cpp
1822 lines (1429 loc) · 45.8 KB
/
miscutil.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ライセンス: GPL2
//#define _DEBUG
#include "jddebug.h"
#include "miscutil.h"
#include "miscmsg.h"
#include "jdiconv.h"
#include "jdregex.h"
#include "hkana.h"
#include "dbtree/spchar_decoder.h"
#include "dbtree/node.h"
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
//
// str を "\n" ごとに区切ってlistにして出力
//
std::list< std::string > MISC::get_lines( const std::string& str ){
std::list< std::string > lines;
size_t i = 0, i2 = 0, r = 0;
while ( ( i2 = str.find( "\n", i ) ) != std::string::npos ){
r = 0;
if( (i2 >= 1) && (str[ i2 - 1 ] == '\r') ) r = 1;
if( i2 - i > 0 ){
lines.push_back( str.substr( i, i2 - i - r ) );
}
i = i2 + 1;
}
// 最後の行
if( i != str.length() +1 ) lines.push_back( str.substr( i ) );
return lines;
}
//
// emacs lisp のリスト型を要素ごとにlistにして出力
//
std::list< std::string > MISC::get_elisp_lists( const std::string& str )
{
#ifdef _DEBUG
std::cout << "MISC::get_elisp_lists\n";
#endif
std::list< std::string > lists;
size_t pos = 0, length = 0;
std::string str2 = remove_space( str );
const char* data = str2.c_str();
if( data[ 0 ] != '(' ) return lists;
pos = 1;
while( data[ pos ] != '\0' ){
// 空白削除
while( data[ pos ] == ' ' && data[ pos ] != '\0' ) ++pos;
if( data[ pos ] == '\0' ) break;
length = 1;
// (が現れた
if( data[ pos ] == '(' ){
int count = 1;
while( data[ pos + length ] != '\0' ){
if( data[ pos + length ] == ')' ) --count;
else if( data[ pos + length ] == '(' ) ++count;
++length;
if( ! count ) break;
}
}
// 改行 or データが壊れてる
else if( data[ pos ] == '\n' || data[ pos ] == ')' ){
++pos;
continue;
}
// 通常データ
else{
//空白 or ) を探す
while( data[ pos + length ] != ' ' && data[ pos + length ] != ')' && data[ pos + length ] != '\0' ) ++length;
}
#ifdef _DEBUG
std::cout << "pos = " << pos << " length = " << length << std::endl;
#endif
lists.push_back( str2.substr( pos, length ) );
pos += length;
}
#ifdef _DEBUG
std::list < std::string >::iterator it;
for( it = lists.begin(); it != lists.end(); ++it ) std::cout << "[" << *it << "]" << std::endl;
#endif
return lists;
}
//
// strを空白または "" 単位で区切って list で出力
//
std::list< std::string > MISC::split_line( const std::string& str )
{
std::string str_space = " ";
size_t lng_space = str_space.length();
bool dquote;
std::list< std::string > list_str;
size_t i = 0, i2 = 0, lng = str.length();
for(;;){
// 空白を取る
while( 1 ){
// 半角
if( str[ i ] == ' ' ) ++i;
// 全角
else if( str[ i ] == str_space[ 0 ] &&
str[ i +1 ] == str_space[ 1 ] &&
( lng_space == 2 || str[ i +2 ] == str_space[ 2 ] ) ) i += lng_space;
else break;
}
// " から始まる ( \"は除く )
dquote = false;
if( str[ i ] == '\"' && (i < 1 || str[ i -1 ] != '\\') ){
dquote = true;
++i;
}
// 空白か " を探す
i2 = i;
size_t lng_tmp = 1;
while( i2 < lng ){
// " 発見( \"は除く )
if( dquote ){
if( str[ i2 ] == '\"' && str[ i2-1 ] != '\\' ) break;
}
else{
// 半角
if( str[ i2 ] == ' ' ) break;
// 全角
else if( str[ i2 ] == str_space[ 0 ] &&
str[ i2 +1 ] == str_space[ 1 ] &&
( lng_space == 2 || str[ i2 +2 ] == str_space[ 2 ] ) ){
lng_tmp = lng_space;
break;
}
}
++i2;
}
if( i2 - i ) list_str.push_back( str.substr( i, i2 - i ) );
if( i2 >= lng ) break;
i = i2 + lng_tmp;
}
return list_str;
}
// strを delimで区切って list で出力
std::list< std::string > MISC::StringTokenizer( const std::string& str, const char delim )
{
std::list< std::string > list_str;
size_t i = 0, i2 = 0, lng = str.length();
for(;;){
while( i2 < lng && str[ i2++ ] != delim );
int tmp = ( i2 >= 1 && ( str[ i2-1 ] == delim || str[ i2 -1 ] == '\n' ) ) ? 1 : 0;
if( i2 - i ) list_str.push_back( str.substr( i, i2 - i - tmp ) );
if( i2 >= lng ) break;
i = i2;
}
return list_str;
}
//
// list_inから空白行を除いてリストを返す
//
std::list< std::string > MISC::remove_nullline_from_list( const std::list< std::string >& list_in )
{
std::list< std::string > list_ret;
std::list< std::string >::const_iterator it;
for( it = list_in.begin(); it != list_in.end(); ++it ){
std::string tmp_str = MISC::remove_space( (*it) );
if( ! tmp_str.empty() ) list_ret.push_back( *it );
}
return list_ret;
}
//
// list_inの各行から前後の空白を除いてリストを返す
//
std::list< std::string > MISC::remove_space_from_list( const std::list< std::string >& list_in )
{
std::list< std::string > list_ret;
std::list< std::string >::const_iterator it;
for( it = list_in.begin(); it != list_in.end(); ++it ){
std::string tmp_str = MISC::remove_space( (*it) );
list_ret.push_back( tmp_str );
}
return list_ret;
}
//
// list_inからコメント行(#)を除いてリストを返す
//
std::list< std::string > MISC::remove_commentline_from_list( const std::list< std::string >& list_in )
{
const char commentchr = '#';
std::list< std::string > list_ret;
std::list< std::string >::const_iterator it;
for( it = list_in.begin(); it != list_in.end(); ++it ){
std::string tmp_str = MISC::remove_space( (*it) );
if( tmp_str.c_str()[ 0 ] != commentchr ) list_ret.push_back( *it );
}
return list_ret;
}
//
// 空白と""で区切られた str_in の文字列をリストにして出力
//
// \"は " に置換される
//
// (例) "aaa" "bbb" "\"ccc\"" → aaa と bbb と "ccc"
//
std::list< std::string > MISC::strtolist( const std::string& str_in )
{
std::list< std::string > list_tmp;
std::list< std::string > list_ret;
list_tmp = MISC::split_line( str_in );
std::list< std::string >::iterator it = list_tmp.begin();
for( ; it != list_tmp.end(); ++it ){
if( !( *it ).empty() ) list_ret.push_back( MISC::recover_quot( ( *it ) ) );
}
return list_ret;
}
//
// list_in の文字列リストを空白と""で区切ってストリングにして出力
//
// "は \" に置換される
//
// (例) "aaa" "bbb" "\"ccc\""
//
std::string MISC::listtostr( const std::list< std::string >& list_in )
{
std::string str_out;
std::list< std::string >::const_iterator it = list_in.begin();
for( ; it != list_in.end(); ++it ){
if( ! ( *it ).empty() ) str_out += " \"" + MISC::replace_quot( ( *it ) ) + "\"";
}
return str_out;
}
//
// strの前後の空白削除
//
std::string MISC::remove_space( const std::string& str )
{
std::string str_space = " ";
size_t lng_space = str_space.length();
size_t lng = str.length();
if( lng == 0 ) return str;
if( str.find( " " ) == std::string::npos ) return str;
// 前
size_t i = 0;
while( 1 ){
// 半角
if( str[ i ] == ' ' ) ++i;
// 全角
else if( str[ i ] == str_space[ 0 ] &&
str[ i +1 ] == str_space[ 1 ] &&
( lng_space == 2 || str[ i +2 ] == str_space[ 2 ] ) ) i += lng_space;
else break;
}
// 後
size_t i2 = lng -1;
while( 1 ){
// 半角
if( str[ i2 ] == ' ' ) --i2;
// 全角
else if( i2 +1 >= lng_space &&
str[ i2 - lng_space +1 ] == str_space[ 0 ] &&
str[ i2 - lng_space +2 ] == str_space[ 1 ] &&
( lng_space == 2 || str[ i2 - lng_space +3 ] == str_space[ 2 ] ) ) i2 -= lng_space;
else break;
}
return str.substr( i, i2 - i + 1 );
}
//
// str前後の改行、タブ、スペースを削除
//
std::string MISC::remove_spaces( const std::string& str )
{
if( str.empty() ) return std::string();
size_t l = 0, r = str.length();
while( l < r
&& ( str[l] == '\n'
|| str[l] == '\r'
|| str[l] == '\t'
|| str[l] == ' ' ) ) ++l;
// 最後の文字の位置は文字数より1少ない
size_t p = r - 1;
while( p > 0
&& ( str[p] == '\n'
|| str[p] == '\r'
|| str[p] == '\t'
|| str[p] == ' ' ) ){ --p; --r; }
return str.substr( l, r - l );
}
//
// str1からstr2で示された文字列を除く
//
std::string MISC::remove_str( const std::string& str1, const std::string& str2 )
{
return MISC::replace_str( str1, str2, "" );
}
//
// start 〜 end の範囲をstrから取り除く ( /* コメント */ など )
//
std::string MISC::remove_str( const std::string& str, const std::string& start, const std::string& end )
{
std::string str_out = str;
size_t l_pos = 0, r_pos = 0;
const size_t start_length = start.length();
const size_t end_length = end.length();
while( ( l_pos = str_out.find( start, l_pos ) ) != std::string::npos &&
( r_pos = str_out.find( end, l_pos + start_length ) ) != std::string::npos )
{
str_out.erase( l_pos, r_pos - l_pos + end_length );
}
return str_out;
}
//
// 正規表現を使ってstr1からqueryで示された文字列を除く
//
std::string MISC::remove_str_regex( const std::string& str1, const std::string& query )
{
JDLIB::Regex regex;
const size_t offset = 0;
const bool icase = false;
const bool newline = true;
const bool usemigemo = false;
const bool wchar = false;
if( ! regex.exec( query, str1, offset, icase, newline, usemigemo, wchar ) ) return std::string();
return MISC::remove_str( str1, regex.str( 0 ) );
}
//
// str1, str2 に囲まれた文字列を切り出す
//
std::string MISC::cut_str( const std::string& str, const std::string& str1, const std::string& str2 )
{
size_t i = str.find( str1 );
if( i == std::string::npos ) return std::string();
i += str1.length();
size_t i2 = str.find( str2, i );
if( i2 == std::string::npos ) return std::string();
return str.substr( i, i2 - i );
}
//
// str1 を str2 に置き換え
//
std::string MISC::replace_str( const std::string& str, const std::string& str1, const std::string& str2 )
{
size_t i, pos = 0;
if( ( i = str.find( str1 , pos ) ) == std::string::npos ) return str;
std::string str_out;
str_out.reserve( str.length() );
do {
str_out.append( str, pos, ( i - pos ) );
str_out.append( str2 );
pos = i + str1.length();
}
while( ( i = str.find( str1 , pos ) ) != std::string::npos );
str_out.append( str, pos, str.length() );
return str_out;
}
//
// list_inから str1 を str2 に置き換えてリストを返す
//
std::list< std::string > MISC::replace_str_list( const std::list< std::string >& list_in,
const std::string& str1, const std::string& str2 )
{
std::list< std::string > list_out;
std::list< std::string >::const_iterator it = list_in.begin();
for( ; it != list_in.end(); ++it ) list_out.push_back( replace_str( *it, str1, str2 ) );
return list_out;
}
//
// str_in に含まれる改行文字を replace に置き換え
//
std::string MISC::replace_newlines_to_str( const std::string& str_in, const std::string& replace )
{
if( str_in.empty() || replace.empty() ) return str_in;
std::string str_out;
str_out.reserve( str_in.length() );
size_t pos = 0, found = 0;
while( ( found = str_in.find_first_of( "\r\n", pos ) ) != std::string::npos )
{
str_out.append( str_in, pos, ( found - pos ) );
str_out.append( replace );
pos = found + 1;
if( str_in[ found ] == '\r' && str_in[ found + 1 ] == '\n' ) ++pos;
}
str_out.append( str_in, pos, str_in.length() );
return str_out;
}
//
// " を \" に置き換え
//
std::string MISC::replace_quot( const std::string& str )
{
return MISC::replace_str( str, "\"", "\\\"" );
}
//
// \" を " に置き換え
//
std::string MISC::recover_quot( const std::string& str )
{
return MISC::replace_str( str, "\\\"", "\"" );
}
//
// str 中に含まれている str2 の 数を返す
//
int MISC::count_str( const std::string& str, const std::string& str2 )
{
int count = 0;
size_t found, pos = 0;
while( ( found = str.find( str2, pos ) ) != std::string::npos )
{
++count;
pos = found + 1;
}
return count;
}
//
// str 中に含まれている chr の 数を返す
//
int MISC::count_chr( const std::string& str, const char chr )
{
int count = 0;
const int size = str.size();
for( int i = 0; i < size; ++i ) if( str.c_str()[i] == chr ) ++count;
return count;
}
//
// 文字列(utf-8も) -> 整数変換
//
// (例) "123" -> 123
//
// 入力:
// str
//
// 出力:
// dig: 桁数、0なら失敗
// n : str から何バイト読み取ったか
//
// 戻り値: 数値
//
int MISC::str_to_uint( const char* str, size_t& dig, size_t& n )
{
int out = 0;
dig = 0;
n = 0;
while( *str != '\0' ){
const unsigned char in = (*str);
if( '0' <= in && in <= '9' ){
out = out*10 + ( in - '0' );
++dig;
++str;
++n;
}
else{
const unsigned char in2 = (* ( str +1 ));
const unsigned char in3 = (* ( str +2 ));
// utf-8
if( in == 0xef && in2 == 0xbc && ( 0x90 <= in3 && in3 <= 0x99 ) ){
out = out*10 + ( in3 - 0x90 );
++dig;
str += 3;
n += 3;
}
else break;
}
}
return out;
}
//
// 数字 -> 文字変換
//
std::string MISC::itostr( const int n )
{
std::ostringstream ss;
ss << n;
return ss.str();
}
//
// listで指定した数字を文字に変換
//
std::string MISC::intlisttostr( const std::list< int >& list_num )
{
std::ostringstream comment;
std::list < int >::const_iterator it = list_num.begin();
bool comma = false;
int num_from = *it;
int num_to = -1;
int i = 0;
for(;;){
++i;
++it;
const int num = *it;
if( num_from + i != num || it == list_num.end() ){
if( comma ) comment << ",";
comment << num_from;
if( num_to != -1 ) comment << "-" << num_to;
num_from = num;
num_to = -1;
i = 0;
comma = true;
if( it == list_num.end() ) break;
}
else num_to = num;
}
return comment.str();
}
//
// 16進数表記文字をバイナリに変換する( 例 "E38182" -> 0xE38182 )
//
// 出力 : char_out
// 戻り値: 変換に成功した chr_in のバイト数
//
size_t MISC::chrtobin( const char* chr_in, char* chr_out )
{
if( ! chr_in ) return 0;
const size_t chr_in_length = strlen( chr_in );
size_t a, b;
for( a = 0, b = a; a < chr_in_length; ++a )
{
unsigned char chr = chr_in[a];
chr_out[b] <<= 4;
// 0(0x30)〜9(0x39)
if( (unsigned char)( chr - 0x30 ) < 10 ) chr_out[b] |= chr - 0x30;
// A(0x41)〜F(0x46)
else if( (unsigned char)( chr - 0x41 ) < 6 ) chr_out[b] |= chr - 0x37;
// a(0x61)〜f(0x66)
else if( (unsigned char)( chr - 0x61 ) < 6 ) chr_out[b] |= chr - 0x57;
// その他
else break;
if( a % 2 != 0 ) ++b;
}
return a;
}
//
// strが半角でmaxsize文字を超えたらカットして後ろに...を付ける
//
std::string MISC::cut_str( const std::string& str, const unsigned int maxsize )
{
std::string outstr = str;
unsigned int pos, lng_str;
int byte = 0;
const size_t outstr_length = outstr.length();
for( pos = 0, lng_str = 0; pos < outstr_length; pos += byte ){
MISC::utf8toucs2( outstr.c_str()+pos, byte );
if( byte > 1 ) lng_str += 2;
else ++lng_str;
if( lng_str >= maxsize ) break;
}
// カットしたら"..."をつける
if( pos != outstr_length ) outstr = outstr.substr( 0, pos ) + "...";
return outstr;
}
//
// 正規表現のメタ文字が含まれているか
//
// escape == true ならエスケープを考慮 (例) escape == true なら \+ → \+ のまま、falseなら \+ → \\\+
//
#define REGEX_METACHARS ".+*?^$|{}[]()\\"
bool MISC::has_regex_metachar( const std::string& str, const bool escape )
{
const char metachars[] = REGEX_METACHARS;
const size_t str_length = str.length();
for( size_t pos = 0; pos < str_length; ++pos ){
if( escape && str[ pos ] == '\\' ){
int i = 0;
while( metachars[ i ] != '\0' ){
if( str[ pos + 1 ] == metachars[ i ] ) break;
++i;
}
if( metachars[ i ] == '\0' ) return true;
++pos;
}
else{
int i = 0;
while( metachars[ i ] != '\0' ){
if( str[ pos ] == metachars[ i ] ) return true;
++i;
}
}
}
return false;
}
//
// 正規表現のメタ文字をエスケープ
//
// escape == true ならエスケープを考慮 (例) escape == true なら \+ → \+ のまま、falseなら \+ → \\\+
//
std::string MISC::regex_escape( const std::string& str, const bool escape )
{
if( ! has_regex_metachar( str, escape ) ) return str;
#ifdef _DEBUG
std::cout << "MISC::regex_escape" << std::endl;
#endif
std::string str_out;
const char metachars[] = REGEX_METACHARS;
const size_t str_length = str.length();
for( size_t pos = 0; pos < str_length; ++pos ){
if( escape && str[ pos ] == '\\' ){
int i = 0;
while( metachars[ i ] != '\0' ){
if( str[ pos + 1 ] == metachars[ i ] ) break;
++i;
}
if( metachars[ i ] == '\0' ) str_out += '\\';
else{
str_out += str[ pos ];
++pos;
}
}
else{
int i = 0;
while( metachars[ i ] != '\0' ){
if( str[ pos ] == metachars[ i ] ){
str_out += '\\';
break;
}
++i;
}
}
str_out += str[ pos ];
}
#ifdef _DEBUG
std::cout << str << " -> " << str_out << std::endl;
#endif
return str_out;
}
//
// 正規表現のメタ文字をアンエスケープ
//
std::string MISC::regex_unescape( const std::string& str )
{
#ifdef _DEBUG
std::cout << "MISC::regex_unescape" << std::endl;
#endif
std::string str_out;
const char metachars[] = REGEX_METACHARS;
const size_t str_length = str.length();
for( size_t pos = 0; pos < str_length; ++pos ){
if( str[ pos ] == '\\' ){
int i = 0;
while( metachars[ i ] != '\0' ){
if( str[ pos + 1 ] == metachars[ i ] ){
++pos;
break;
}
++i;
}
}
str_out += str[ pos ];
}
#ifdef _DEBUG
std::cout << str << " -> " << str_out << std::endl;
#endif
return str_out;
}
//
// HTMLエスケープ
//
// include_url : URL中でもエスケープする( デフォルト = true )
//
std::string MISC::html_escape( const std::string& str, const bool include_url )
{
if( str.empty() ) return str;
bool is_url = false;
int scheme = SCHEME_NONE;
std::string str_out;
const size_t str_length = str.length();
for( size_t pos = 0; pos < str_length; ++pos )
{
char tmpchar = str.c_str()[ pos ];
// URL中はエスケープしない場合
if( ! include_url )
{
// URLとして扱うかどうか
// エスケープには影響がないので loose_url としておく
if( scheme != SCHEME_NONE ) is_url = is_url_char( str.c_str() + pos, true );
// URLスキームが含まれているか判別
int len = 0;
if( ! is_url ) scheme = is_url_scheme( str.c_str() + pos, &len );
// URLスキームが含まれていた場合は文字数分進めてループに戻る
if( len > 0 )
{
str_out += str.substr( pos, len );
pos += len - 1; // あとで ++pos される分を引く
continue;
}
}
// include_url = false でURL中ならエスケープしない
if( is_url ) str_out += tmpchar;
else if( tmpchar == '&' )
{
const int bufsize = 64;
char out_char[ bufsize ];
int n_in, n_out;
const int type = DBTREE::decode_char( str.c_str() + pos, n_in, out_char, n_out, false );
if( type == DBTREE::NODE_NONE ) str_out += "&";
else str_out += tmpchar;
}
else if( tmpchar == '\"' ) str_out += """;
else if( tmpchar == '<' ) str_out += "<";
else if( tmpchar == '>' ) str_out += ">";
else str_out += tmpchar;
}
#ifdef _DEBUG
if( str != str_out ){
std::cout << "MISC::html_escape\nstr = " << str << std::endl
<< "out = " << str_out << std::endl;
}
#endif
return str_out;
}
//
// HTMLアンエスケープ
//
std::string MISC::html_unescape( const std::string& str )
{
if( str.empty() ) return str;
if( str.find( "&" ) == std::string::npos ) return str;
std::string str_out;
const size_t str_length = str.length();
for( size_t pos = 0; pos < str_length; ++pos ){
const int bufsize = 64;
char out_char[ bufsize ];
int n_in, n_out;
DBTREE::decode_char( str.c_str() + pos, n_in, out_char, n_out, false );
if( n_out ){
str_out += out_char;
pos += n_in -1;
}
else str_out += str.c_str()[ pos ];
}
#ifdef _DEBUG
if( str != str_out ){
std::cout << "MISC::html_unescape\nstr = " << str << std::endl
<< "out = " << str_out << std::endl;
}
#endif
return str_out;
}
//
// URL中のスキームを判別する
//
// 戻り値 : スキームタイプ
// length : "http://"等の文字数
//
int MISC::is_url_scheme_impl( const char* str_in, int* length )
{
int scheme = SCHEME_NONE;
int len = 0;
// http https
if( *str_in == 'h' && *( str_in + 1 ) == 't'
&& *( str_in + 2 ) == 't' && *( str_in + 3 ) == 'p' )
{
scheme = SCHEME_HTTP;
len = 4;
if( *( str_in + len ) == 's' ) ++len;
}
// ftp
else if( *str_in == 'f' && *( str_in + 1 ) == 't' && *( str_in + 2 ) == 'p' )
{
scheme = SCHEME_FTP;
len = 3;
}
// ttp ttps
else if( *str_in == 't' && *( str_in + 1 ) == 't' && *( str_in + 2 ) == 'p' )
{
scheme = SCHEME_TTP;
len = 3;
if( *( str_in + len ) == 's' ) ++len;
}
// tp tps
else if( *str_in == 't' && *( str_in + 1 ) == 'p' )
{
scheme = SCHEME_TP;
len = 2;
if( *( str_in + len ) == 's' ) ++len;
}
// sssp
// デフォルトでモザイクを解除するのでimg.2ch以外のアドレスにはリンクを張らない
else if( *str_in == 's' && *( str_in + 1 ) == 's' && *( str_in + 2 ) == 's' && *( str_in + 3 ) == 'p'
&& *( str_in + 7 ) == 'i' && *( str_in + 8 ) == 'm' && *( str_in + 9 ) == 'g' && *( str_in + 10 ) == '.'
&& *( str_in + 11 ) == '2' && *( str_in + 12 ) == 'c' && *( str_in + 13 ) == 'h'
)
{
scheme = SCHEME_SSSP;
len = 4;
}
// 各スキーム後に続く共通の"://"
if( *( str_in + len ) == ':' && *( str_in + len + 1 ) == '/'
&& *( str_in + len + 2 ) == '/' )
{
len += 3;
if( length ) *length = len;