2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: String.cpp,v 1.65 2004/05/18 12:47:17 randy Exp $
22 void SpString::append(const char* s)
24 //cout << "spstr.app('" << s << "')" << endl;
26 StreamReader reader(&ist);
28 SpChar ch = reader.ReadChar();
29 if(ch == (SpChar)NULL){ break; }
35 //void SpString::appendWithEncoder(const char* s, ReadEncoder& encoder)
36 void SpString::append(const char* s, ReadEncoder& encoder)
38 //cout << "spstr.app('" << s << "')" << endl;
40 StreamReader sr(&ist);
41 Reader* old = encoder.getReader();
42 encoder.setReader(&sr);
43 while(!encoder.eof()){
44 SpChar ch = encoder.ReadChar();
45 if(ch == (SpChar)NULL){ break; }
48 encoder.setReader(old);
54 *spout << ":destroy SpString '" << toCStringWithEncoder() << "'" << "\n";
58 const int Buf_Size = 1024 * 4;
61 const char* SpString::toCString()
64 StreamWriter writer(&ost);
65 SpCharVector::iterator it;
66 for(it = str.begin(); it < str.end(); it++){
67 writer.WriteChar(*it);
76 const char* SpString::toCStringWithEncoder(WriteEncoder& encoder)
79 StreamWriter sw(&ost);
80 Writer* old = encoder.getWriter();
81 encoder.setWriter(&sw);
82 SpCharVector::iterator it;
83 for(it = str.begin(); it < str.end(); it++){
84 encoder.WriteChar(*it);
86 encoder.setWriter(old);
93 SpValue& SpString::toString()
95 SpString* str = new SpString("\"");
96 *str += toCStringWithEncoder();
98 // static SpValue val;
99 // val.setNewObject(str);
101 return SpObjectResult(str);
104 bool SpString::operator==(SpObject& obj)
106 //cout << "str== " << *this << " <-> " << obj << endl;
107 if(SpString* s = dynamic_cast<SpString*>(&obj)){
108 return str == s->str;
113 bool SpString::operator<(SpObject& obj)
115 //cout << "str== " << *this << " <-> " << obj << endl;
116 if(typeid(*this) == typeid(obj)){
117 if(SpString* s = dynamic_cast<SpString*>(&obj)){
121 return SpObject::operator<(obj);
124 SpString& SpString::operator+=(SpString& s)
126 SpCharVector::iterator it;
127 for(it = s.str.begin(); it != s.str.end(); it++){
133 SpValue& SpString::plus(SpValue& e1, SpValue& e2)
140 throw SpException("type mismatch in +(String, ...)");
142 s1 = dynamic_cast<SpString*>(e1.getObject());
143 s2 = dynamic_cast<SpString*>(e2.getObject());
144 s3 = new SpString(*s1);
147 // v.setNewObject(s3);
149 return SpObjectResult(s3);
152 bool SpString::match(SpValue&, SpValue& val, SpNameSpace*)
155 return operator==(*(val.getObject()));
160 SpValue& SpString::eq(SpValue&, SpValue& e2)
163 //throw SpException("type mismatch in ==(String, ...)");
166 SpString* s = e2.asString();
173 SpValue& SpString::gt(SpValue&, SpValue& e2)
176 throw SpException("type mismatch in >(String, ...)");
178 SpString* s = e2.asString();
185 SpValue& SpString::lt(SpValue&, SpValue& e2)
188 throw SpException("type mismatch in <(String, ...)");
190 SpString* s = e2.asString();
197 //
\83p
\83X
\82ð
\83f
\83B
\83\8c\83N
\83g
\83\8a\82Æ
\83t
\83@
\83C
\83\8b\96¼
\82É
\95ª
\82¯
\82é
\81B
198 SpTuple* SpString::split_path_file()
200 SpCharVector::iterator it = begin();
201 SpCharVector::iterator last = end();
203 for(; it < end(); it++){
205 if((*it == JIS('\\')) ||
218 tuple = new SpTuple(NullString);
219 temp.setNewObject(this);
222 SpString* pstr = new SpString; // path
223 SpString* fstr = new SpString; // filename
224 for(it = begin(); it < last; it++){
225 pstr->addSpChar(*it);
227 for(it++; it < end(); it++){
228 fstr->addSpChar(*it);
232 temp.setNewObject(pstr);
233 tuple = new SpTuple(temp);
234 temp.setNewObject(fstr);
241 static bool string_match_glob(SpCharVector::iterator sbegin,
242 SpCharVector::iterator send,
243 SpCharVector::iterator gbegin,
244 SpCharVector::iterator gend)
246 SpCharVector::iterator it = sbegin;
247 SpCharVector::iterator git = gbegin;
249 for(git; git < gend; git++){
259 if(*git == JIS('*')){
260 throw SpException("illegal glob '**'");
268 if(*git == JIS('?')){
273 if(string_match_glob(it, send, git, gend)){
287 throw SpException("illegal end (glob)");
308 bool SpString::match_glob(SpString* glob)
310 return string_match_glob(begin(), end(), glob->begin(), glob->end());
314 SpValue& SpString::split(SpChar c)
322 SpCharVector::iterator it = begin();;
325 for(; it < end(); it++){
333 s2 = new SpString(s);
335 temp.setNewObject(s2);
337 cons = new SpCons(temp);
343 // static SpValue result;
344 // result.setNewObject(cons);
346 return SpObjectResult(cons);
353 SpValue& SpString::onMessage(SpValue& rec, SpValue& msg)
355 return StringMsgHandler(rec, msg);
360 SpValue& str_length(SpValue& s)
362 SpString* ptr = dynamic_cast<SpString*>(s.getObject());
364 throw SpException("not string (length)");
367 // v.setInt(ptr->length());
369 return SpIntResult(ptr->length());
372 SpValue& SpString::prim_nth(SpValue& self, SpValue& index)
375 throw SpException("not int. (tuple.nth)");
377 SpInt i = index.getInt();
378 SpString* str = self.asString();
379 if(str->length() < i + 1){
380 throw SpException("size over (string.nth)");
382 // static SpValue result;
383 // result.setSpChar((*str)[i]);
385 return SpCharResult((*str)[i]);
388 SpValue& SpString::prim_sub(SpValue& self, SpValue& from, SpValue& len)
391 throw SpException("not int. (string.sub)");
394 throw SpException("not int. (string.sub)");
396 SpInt i = from.getInt();
397 SpInt j = len.getInt();
401 SpString* str = self.asString();
402 if(str->length() < i+1){
403 throw SpException("string is too short (string.sub)");
405 if(str->length() < i+j){
406 throw SpException("string is too short. (string.sub)");
410 for(int k=0; k < j; k++){
411 vec.push_back(str->str[i+k]);
413 SpString* s = new SpString(vec);
414 // static SpValue result;
415 // result.setNewObject(s);
417 return SpObjectResult(s);
420 SpValue& SpString::prim_split(SpValue& self, SpValue& ch)
423 throw SpException("not char(string.split)");
425 SpChar c = ch.getChar();
426 SpString* str = self.asString();
428 // static SpValue result;
429 // result = str->split(c);
431 return SpValueResult(str->split(c));
434 SpValue& SpString::prim_split1(SpValue& self, SpValue& ch)
437 throw SpException("not char(string.split1)");
439 SpChar c = ch.getChar();
440 SpString* str = self.asString();
442 if(str->length() == 0){
448 SpCharVector::iterator it = str->begin();;
450 for(; it < str->end(); it++){
458 s2 = new SpString(s);
460 temp.setNewObject(s2);
461 cons = new SpCons(temp);
464 for(; it < str->end(); it++){
467 s2 = new SpString(s);
468 temp.setNewObject(s2);
471 cons->append(NullString);
474 // static SpValue result;
475 // result.setNewObject(cons);
477 return SpObjectResult(cons);
480 SpValue& SpString::prim_tr(SpValue& self, SpValue& from, SpValue& to)
483 throw SpException("not char(string.tr)");
486 throw SpException("not char(string.tr)");
488 SpChar c1 = from.getChar();
489 SpChar c2 = to.getChar();
490 SpString* str = self.asString();
492 SpString* s = new SpString;
493 SpCharVector::iterator it;
494 for(it = str->begin(); it < str->end(); it++){
502 // static SpValue result;
503 // result.setNewObject(s);
505 return SpObjectResult(s);
508 SpValue& SpString::prim_reader(SpValue& self)
510 SpString* str = self.asString();
511 StringReader* reader = new StringReader(str);
513 // static SpValue result;
514 // result.setNewObject(reader);
516 return SpObjectResult(reader);
519 SpValue& SpString::prim_upper(SpValue& self)
521 SpString* str = self.asString();
523 SpString* s = new SpString;
524 SpCharVector::iterator it;
525 for(it = str->begin(); it < str->end(); it++){
526 s->addSpChar(SpChar2Upper(*it));
529 // static SpValue result;
530 // result.setNewObject(s);
532 return SpObjectResult(s);
535 SpValue& SpString::prim_lower(SpValue& self)
537 SpString* str = self.asString();
539 SpString* s = new SpString;
540 SpCharVector::iterator it;
541 for(it = str->begin(); it < str->end(); it++){
542 s->addSpChar(SpChar2Lower(*it));
545 // static SpValue result;
546 // result.setNewObject(s);
548 return SpObjectResult(s);
551 SpValue& str2symbol(SpValue& s)
553 SpString* ptr = dynamic_cast<SpString*>(s.getObject());
555 throw SpException("not string (string.symbol)");
558 // v.setNewObject(new SpSymbol(*ptr));
560 return SpObjectResult(new SpSymbol(*ptr));
563 SpValue& SpString::prim_toInt(SpValue& self)
565 SpString* str = self.asString();
568 SpCharVector::iterator it = str->str.begin();
569 for(; it < str->str.end(); it++){
572 throw SpException("not digit (string.toInt)");
574 n = n * 10 + (c - JIS('0'));
577 // static SpValue result;
580 return SpIntResult(n);
583 SpValue& SpString::prim_replace(SpValue& self, SpValue& from, SpValue& to)
585 if(!from.isString()){
586 throw SpException("not string(string.replace)");
589 throw SpException("not string(string.replace)");
591 SpString* src = self.asString();
592 SpString* s1 = from.asString();
593 SpString* s2 = to.asString();
595 SpString* res = new SpString;
596 SpCharVector::iterator sit; // source iterator
597 SpCharVector::iterator fit; // from iterator
601 while(sit < src->end()){
603 bool all_match = true;
604 SpCharVector::iterator it1;
605 SpCharVector::iterator it2;
610 while(it2 < s1->end()){
619 SpCharVector::iterator it3 = s2->begin();
620 for(; it3 < s2->end(); it3++){
621 res->addSpChar(*it3);
626 res->addSpChar(*sit);
630 res->addSpChar(*sit);
635 // static SpValue result;
636 // result.setNewObject(res);
638 return SpObjectResult(res);
641 SpValue& SpString::prim_replace1(SpValue& self, SpValue& from, SpValue& to)
643 if(!from.isString()){
644 throw SpException("not string(string.replace1)");
647 throw SpException("not string(string.replace1)");
649 SpString* src = self.asString();
650 SpString* s1 = from.asString();
651 SpString* s2 = to.asString();
653 SpString* res = new SpString;
654 SpCharVector::iterator sit; // source iterator
655 SpCharVector::iterator fit; // from iterator
659 while(sit < src->end()){
661 bool all_match = true;
662 SpCharVector::iterator it1;
663 SpCharVector::iterator it2;
668 while(it2 < s1->end()){
677 SpCharVector::iterator it3 = s2->begin();
678 for(; it3 < s2->end(); it3++){
679 res->addSpChar(*it3);
682 for(; sit < src->end(); sit++){
683 res->addSpChar(*sit);
687 res->addSpChar(*sit);
691 res->addSpChar(*sit);
696 // static SpValue result;
697 // result.setNewObject(res);
699 return SpObjectResult(res);
702 SpValue& SpString::prim_find(SpValue& self, SpValue& s, SpValue& from)
704 // static SpValue result;
708 throw SpException("not string(string.find)");
711 throw SpException("not int(string.find)");
713 SpString* src = self.asString();
714 SpString* s1 = s.asString();
715 SpInt start = from.getInt();
717 SpCharVector::iterator it = src->begin();;
723 for(; *it != s1->str[0]; it++, i++){
724 if(i >= src->length()){
727 return SpValueResult(result);
731 SpCharVector::iterator it2 = it;
733 for(j=1; j < s1->length(); j++, it2++)
735 if(i+j >= src->length()){
739 if(*it2 != s1->str[j]){
747 return SpValueResult(result);
752 // ex. "format string %s %d %f\n" format aString aInt aReal;
754 static SpValue PrimAdd;
755 static SpValue& _prim_add(SpValue& a, SpValue& b)
757 SpValue result, temp1, temp2;
760 result = temp1.plus(temp2);
761 return SpValueResult(result);
765 class _format_make_string : public SpFunc {
767 bool flag; // '-' flag(
\8d¶
\8bl)
772 _format_make_string(SpChar ch, int len, bool f)
773 : preChar(ch), width(len), flag(f)
777 virtual SpValue& operator()(SpValue& arg);
779 const char* typeString(){ return "_format_make_string"; }
782 SpValue& _format_make_string::toString()
784 SpString* str = new SpString("<make format string>");
785 return SpObjectResult(str);
788 SpValue& _format_make_string::operator()(SpValue& arg)
795 temp = arg.toString();
796 str = temp.asString();
798 str = arg.asString();
800 int len = str->length();
801 if((width == 0) || (width <= len)){
802 result.setObject(str);
803 }else{ // width > len
804 if(flag){ //
\8d¶
\8bl
805 SpString* str2 = new SpString();
807 for(int i=0; i < width - len; i++){
808 //str2->addSpChar(preChar);
809 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
811 result.setObject(str2);
813 SpString* str2 = new SpString();
814 for(int i=0; i < width - len; i++){
815 str2->addSpChar(preChar);
818 result.setObject(str2);
822 return SpValueResult(result);
825 class _format_make_string_object : public _format_make_string {
827 _format_make_string_object(SpChar ch, int len, bool f) : _format_make_string(ch, len, f) {}
828 SpValue& operator()(SpValue& arg){
829 SpValue temp = arg.toString();
830 return _format_make_string::operator()(temp);
835 class _format_make_unsigned_string : public _format_make_string {
837 _format_make_unsigned_string(SpChar ch, int len, bool f) : _format_make_string(ch, len, f) {}
838 SpValue& operator()(SpValue& arg);
841 SpValue& _format_make_unsigned_string::operator()(SpValue& arg)
844 throw SpException("not int in string.format '%u'");
846 SpInt n = arg.getInt();
848 sprintf(buf, "%u", n);
850 temp.setNewObject(new SpString(buf));
851 return _format_make_string::operator()(temp);
855 class _format_make_char_string : public _format_make_string {
857 _format_make_char_string(SpChar ch, int w, bool f)
858 : _format_make_string(ch, w, f) {}
859 SpValue& operator()(SpValue& arg);
862 SpString* str = new SpString("<make format string:char>");
863 return SpObjectResult(str);
865 const char* typeString(){ return "_format_make_char_string"; }
868 SpValue& _format_make_char_string::operator()(SpValue& arg)
874 str = new SpString();
875 temp.setNewObject(str);
877 str->addSpChar((SpChar)n);
879 return _format_make_string::operator()(temp);
883 class _format_make_real_string : public SpFunc {
887 _format_make_real_string(char* str)
890 strncpy(format, str, 63);
892 SpValue& operator()(SpValue& arg);
895 SpString* str = new SpString("<make format string:real>");
896 return SpObjectResult(str);
898 const char* typeString(){ return "_format_make_real_string"; }
901 SpValue& _format_make_real_string::operator()(SpValue& arg)
908 arg.setReal((SpReal)arg.getInt());
911 throw SpException("arg is not real in format string");
913 real = arg.getReal();
914 sprintf(buf, format, real);
915 result.setNewObject(new SpString(buf));
917 return SpValueResult(result);
921 class _format_make_b_string : public SpFunc {
929 _format_make_b_string(int w, bool s, bool m, bool z)
930 : width(w), fSharp(s), fMinus(m), fZero(z)
934 SpValue& operator()(SpValue& arg);
937 SpString* str = new SpString("<make format string:b>");
938 return SpObjectResult(str);
940 const char* typeString(){ return "_format_make_b_string"; }
944 #define INT_BIT (CHAR_BIT * (sizeof(int) / sizeof(char)))
947 SpValue& _format_make_b_string::operator()(SpValue& arg)
952 SpString* str = NULL;
955 throw SpException("not int '%b'");
959 for(int i = INT_BIT-1; i >= 0; i--){
960 if(((n >> i) & 1) == 1){
962 str = new SpString("1");
963 result.setObject(str);
965 str->addSpChar(MakeSpChar(CodeJIS, '1'));
969 str->addSpChar(MakeSpChar(CodeJIS, '0'));
974 str = new SpString();
975 result.setObject(str);
978 int len = str->length();
979 if(fSharp){ // '#' flag
980 if((width - 2) > len){
982 if(fMinus){ //
\8d¶
\8bl
983 str2 = new SpString("0b");
985 for(int i=0; i < (width - 2) - len; i++){
986 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
990 str2 = new SpString("0b");
991 for(int i=0; i < (width - 2) - len; i++){
992 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
995 str2 = new SpString();
996 for(int i=0; i < (width - 2) - len; i++){
997 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
999 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1000 str2->addSpChar(MakeSpChar(CodeJIS, 'b'));
1004 result.setObject(str2);
1006 SpString* str2 = new SpString("0b");
1008 temp.setObject(str2);
1010 result.setObject(str2);
1014 SpString* str2 = new SpString();
1015 if(fMinus){ //
\8d¶
\8bl
1017 for(int i=0; i < width - len; i++){
1018 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1023 preChar = MakeSpChar(CodeJIS, '0');
1025 preChar = MakeSpChar(CodeJIS, ' ');
1027 for(int i=0; i < width - len; i++){
1028 str2->addSpChar(preChar);
1032 result.setObject(str2);
1036 return SpValueResult(result);
1040 class _format_make_o_string : public SpFunc {
1048 _format_make_o_string(int w, bool s, bool m, bool z)
1049 : width(w), fSharp(s), fMinus(m), fZero(z)
1053 SpValue& operator()(SpValue& arg);
1056 SpString* str = new SpString("<make format string:o>");
1057 return SpObjectResult(str);
1059 const char* typeString(){ return "_format_make_o_string"; }
1062 SpValue& _format_make_o_string::operator()(SpValue& arg)
1070 throw SpException("not int '%o'");
1072 n = (SpUInt)arg.getInt();
1074 str = new SpString();
1075 result.setNewObject(str);
1076 for(SpUInt i = n; i > 0; i >>= 3){
1078 str->insertSpChar(MakeSpChar(CodeJIS, j + '0'));
1080 if(str->length() == 0){
1081 str->addSpChar(MakeSpChar(CodeJIS, '0'));
1084 int len = str->length();
1085 if(fSharp){ // '#' flag
1086 if((width - 2) > len){
1088 if(fMinus){ //
\8d¶
\8bl
1089 str2 = new SpString("00");
1091 for(int i=0; i < (width - 2) - len; i++){
1092 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1096 str2 = new SpString("00");
1097 for(int i=0; i < (width - 2) - len; i++){
1098 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1101 str2 = new SpString();
1102 for(int i=0; i < (width - 2) - len; i++){
1103 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1105 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1106 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1110 result.setObject(str2);
1112 SpString* str2 = new SpString("00");
1114 temp.setObject(str2);
1116 result.setObject(str2);
1120 SpString* str2 = new SpString();
1121 if(fMinus){ //
\8d¶
\8bl
1123 for(int i=0; i < width - len; i++){
1124 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1129 preChar = MakeSpChar(CodeJIS, '0');
1131 preChar = MakeSpChar(CodeJIS, ' ');
1133 for(int i=0; i < width - len; i++){
1134 str2->addSpChar(preChar);
1138 result.setObject(str2);
1142 return SpValueResult(result);
1146 class _format_make_hex_string : public SpFunc {
1155 _format_make_hex_string(int w, bool up, bool s, bool m, bool z)
1156 : width(w), upper(up), fSharp(s), fMinus(m), fZero(z)
1160 SpValue& operator()(SpValue& arg);
1163 SpString* str = new SpString("<make format string:x(X)>");
1164 return SpObjectResult(str);
1166 const char* typeString(){ return "_format_make_hex_string"; }
1169 SpValue& _format_make_hex_string::operator()(SpValue& arg)
1177 throw SpException("not int '%x' or '%X'");
1179 n = (SpUInt)arg.getInt();
1181 str = new SpString();
1182 result.setNewObject(str);
1183 for(SpUInt i = n; i > 0; i >>= 4){
1186 str->insertSpChar(MakeSpChar(CodeJIS, j + '0'));
1189 str->insertSpChar(MakeSpChar(CodeJIS, j - 10 + 'A'));
1191 str->insertSpChar(MakeSpChar(CodeJIS, j - 10 + 'a'));
1195 if(str->length() == 0){
1196 str->addSpChar(MakeSpChar(CodeJIS, '0'));
1199 int len = str->length();
1200 if(fSharp){ // '#' flag
1201 if((width - 2) > len){
1203 if(fMinus){ //
\8d¶
\8bl
1205 str2 = new SpString("0X");
1207 str2 = new SpString("0x");
1210 for(int i=0; i < (width - 2) - len; i++){
1211 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1216 str2 = new SpString("0X");
1218 str2 = new SpString("0x");
1220 for(int i=0; i < (width - 2) - len; i++){
1221 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1224 str2 = new SpString();
1225 for(int i=0; i < (width - 2) - len; i++){
1226 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1228 str2->addSpChar(MakeSpChar(CodeJIS, '0'));
1230 str2->addSpChar(MakeSpChar(CodeJIS, 'X'));
1232 str2->addSpChar(MakeSpChar(CodeJIS, 'x'));
1237 result.setObject(str2);
1241 str2 = new SpString("0X");
1243 str2 = new SpString("0x");
1246 temp.setObject(str2);
1248 result.setObject(str2);
1252 SpString* str2 = new SpString();
1253 if(fMinus){ //
\8d¶
\8bl
1255 for(int i=0; i < width - len; i++){
1256 str2->addSpChar(MakeSpChar(CodeJIS, ' '));
1261 preChar = MakeSpChar(CodeJIS, '0');
1263 preChar = MakeSpChar(CodeJIS, ' ');
1265 for(int i=0; i < width - len; i++){
1266 str2->addSpChar(preChar);
1270 result.setObject(str2);
1274 return SpValueResult(result);
1278 SpValue& SpString::prim_format(SpValue& self)
1282 bool flagSharp = false;
1283 bool flagPlus = false;
1284 bool flagSpace = false;
1285 bool flagMinus = false;
1286 bool flagZero = false;
1287 bool upper_hex = true;
1288 int width = -1; // -1
\82Ì
\82Æ
\82«
\82É
\82Í
\95\9d\82Ì
\8ew
\92è
\82Í
\96³
\82µ
1289 int width2 = -1; // -1
\82Ì
\82Æ
\82«
\82É
\82Í
\95\9d\82Ì
\8ew
\92è
\82Í
\96³
\82µ
\81A
\82½
\82¾
\82µ
\90¸
\93x
\82Ì
\8fÈ
\97ª
\92l
\82Í
\82U
1291 SpCons* body = NULL;
1292 SpCons* exps = NULL;
1293 SpFunc* func = NULL;
1304 SpString* ptr = dynamic_cast<SpString*>(self.getObject());
1306 throw SpException("not string (format)");
1308 // parse format string
1309 SpCharVector::iterator it;
1310 it = ptr->str.begin();
1311 for(; it != ptr->str.end(); it++){
1321 if(*it == MakeSpChar(CodeJIS, '%')){ // parse '%'
1323 if(it == ptr->str.end()){
1324 throw SpException("illegal format string");
1326 if(*it == MakeSpChar(CodeJIS, '%')){ // parse "%%"
1331 // init preChar & width
1332 preChar = MakeSpChar(CodeJIS, ' ');
1334 temp.setNewObject(new SpString(vec));
1337 exps = new SpCons(temp);
1342 // parse flag ('#', '+', ' ', '-', '0')
1345 case MakeSpChar(CodeJIS, '#'):
1348 if(it == ptr->str.end()){
1349 throw SpException("illegal format string");
1353 case MakeSpChar(CodeJIS, '+'):
1356 if(it == ptr->str.end()){
1357 throw SpException("illegal format string");
1361 case MakeSpChar(CodeJIS, ' '):
1364 if(it == ptr->str.end()){
1365 throw SpException("illegal format string");
1369 case MakeSpChar(CodeJIS, '-'):
1372 if(it == ptr->str.end()){
1373 throw SpException("illegal format string");
1377 case MakeSpChar(CodeJIS, '0'):
1381 if(it == ptr->str.end()){
1382 throw SpException("illegal format string");
1390 for(it++; it != ptr->str.end() && isDIGIT(*it); it++){
1391 width = width * 10 + toINT(*it);
1395 // parser width2 '.num'
1396 if(*it == MakeSpChar(CodeJIS, '.')){
1399 for(; it != ptr->str.end() && isDIGIT(*it); it++){
1400 width2 = width2 * 10 + toINT(*it);
1405 case MakeSpChar(CodeJIS, 'c'): // char
1407 _snprintf(buf, 60, "a%d", count++);
1409 snprintf(buf, 60, "a%d", count++);
1411 sym = new SpSymbol(buf);
1412 temp2.setNewObject(sym);
1413 temp.setNewObject(new _format_make_char_string(preChar, width, flagMinus));
1414 temp.setNewObject(new SpSendMsg(temp, temp2));
1416 temp3.setNewObject(new SpArg(temp2, NilObject));
1417 args.push_back(temp3);
1419 case MakeSpChar(CodeJIS, 's'): // string
1420 case MakeSpChar(CodeJIS, 'p'):
1422 _snprintf(buf, 60, "a%d", count++);
1424 snprintf(buf, 60, "a%d", count++);
1426 sym = new SpSymbol(buf);
1427 temp2.setNewObject(sym);
1428 temp.setNewObject(new _format_make_string(preChar, width, flagMinus));
1429 temp.setNewObject(new SpSendMsg(temp, temp2));
1431 temp3.setNewObject(new SpArg(temp2, NilObject));
1432 args.push_back(temp3);
1434 case MakeSpChar(CodeJIS, 'd'): // digit
1435 case MakeSpChar(CodeJIS, 'i'):
1437 _snprintf(buf, 60, "a%d", count++);
1439 snprintf(buf, 60, "a%d", count++);
1441 sym = new SpSymbol(buf);
1442 temp2.setNewObject(sym);
1443 temp.setNewObject(new _format_make_string_object(preChar, width, flagMinus));
1444 temp.setNewObject(new SpSendMsg(temp, temp2));
1446 temp3.setNewObject(new SpArg(temp2, NilObject));
1447 args.push_back(temp3);
1449 case MakeSpChar(CodeJIS, 'u'): // unsigned int
1451 _snprintf(buf, 60, "a%d", count++);
1453 snprintf(buf, 60, "a%d", count++);
1455 sym = new SpSymbol(buf);
1456 temp2.setNewObject(sym);
1457 temp.setNewObject(new _format_make_unsigned_string(preChar, width, flagMinus));
1458 temp.setNewObject(new SpSendMsg(temp, temp2));
1460 temp3.setNewObject(new SpArg(temp2, NilObject));
1461 args.push_back(temp3);
1463 case MakeSpChar(CodeJIS, 'b'): // bin
1465 _snprintf(buf, 60, "a%d", count++);
1467 snprintf(buf, 60, "a%d", count++);
1469 sym = new SpSymbol(buf);
1470 temp2.setNewObject(sym);
1471 temp.setNewObject(new _format_make_b_string(width, flagSharp, flagMinus, flagZero));
1472 temp.setNewObject(new SpSendMsg(temp, temp2));
1474 temp3.setNewObject(new SpArg(temp2, NilObject));
1475 args.push_back(temp3);
1477 case MakeSpChar(CodeJIS, 'o'): // oct
1479 _snprintf(buf, 60, "a%d", count++);
1481 snprintf(buf, 60, "a%d", count++);
1483 sym = new SpSymbol(buf);
1484 temp2.setNewObject(sym);
1485 temp.setNewObject(new _format_make_o_string(width, flagSharp, flagMinus, flagZero));
1486 temp.setNewObject(new SpSendMsg(temp, temp2));
1488 temp3.setNewObject(new SpArg(temp2, NilObject));
1489 args.push_back(temp3);
1491 case MakeSpChar(CodeJIS, 'x'): // hex
1493 case MakeSpChar(CodeJIS, 'X'): // HEX
1495 _snprintf(buf, 60, "a%d", count++);
1497 snprintf(buf, 60, "a%d", count++);
1499 sym = new SpSymbol(buf);
1500 temp2.setNewObject(sym);
1501 temp.setNewObject(new _format_make_hex_string(width, upper_hex, flagSharp, flagMinus, flagZero));
1502 temp.setNewObject(new SpSendMsg(temp, temp2));
1504 temp3.setNewObject(new SpArg(temp2, NilObject));
1505 args.push_back(temp3);
1507 case MakeSpChar(CodeJIS, 'f'):
1508 case MakeSpChar(CodeJIS, 'e'):
1509 case MakeSpChar(CodeJIS, 'E'):
1510 case MakeSpChar(CodeJIS, 'g'):
1511 case MakeSpChar(CodeJIS, 'G'):
1513 _snprintf(buf, 60, "a%d", count++);
1515 snprintf(buf, 60, "a%d", count++);
1517 sym = new SpSymbol(buf);
1518 temp2.setNewObject(sym);
1524 sprintf(format, "%%#.%d%c", width2, SpCharGetChar(*it));
1526 sprintf(format, "%%+.%d%c", width2, SpCharGetChar(*it));
1527 }else if(flagSpace){
1528 sprintf(format, "%% .%d%c", width2, SpCharGetChar(*it));
1529 }else if(flagMinus){
1530 sprintf(format, "%%-.%d%c", width2, SpCharGetChar(*it));
1532 sprintf(format, "%%0.%d%c", width2, SpCharGetChar(*it));
1534 sprintf(format, "%%.%d%c", width2, SpCharGetChar(*it));
1538 sprintf(format, "%%#%d.%d%c", width, width2, SpCharGetChar(*it));
1540 sprintf(format, "%%+%d.%d%c", width, width2, SpCharGetChar(*it));
1541 }else if(flagSpace){
1542 sprintf(format, "%% %d.%d%c", width, width2, SpCharGetChar(*it));
1543 }else if(flagMinus){
1544 sprintf(format, "%%-%d.%d%c", width, width2, SpCharGetChar(*it));
1546 sprintf(format, "%%0%d.%d%c", width, width2, SpCharGetChar(*it));
1548 sprintf(format, "%%%d.%d%c", width, width2, SpCharGetChar(*it));
1551 temp.setNewObject(new _format_make_real_string(format));
1552 temp.setNewObject(new SpSendMsg(temp, temp2));
1554 temp3.setNewObject(new SpArg(temp2, NilObject));
1555 args.push_back(temp3);
1558 throw SpException("illegal format string");
1567 temp.setNewObject(new SpString(vec));
1568 body = new SpCons(temp);
1572 temp.setNewObject(new SpString(vec));
1574 recv.setObject(exps);
1575 sm = new SpSendMsg(recv, SymFoldl);
1577 sm = new SpSendMsg(recv, PrimAdd);
1578 temp.setNewObject(sm);
1579 body = new SpCons(temp);
1585 recv.setObject(exps);
1586 sm = new SpSendMsg(recv, SymFoldl);
1588 sm = new SpSendMsg(recv, PrimAdd);
1589 temp.setNewObject(sm);
1590 body = new SpCons(temp);
1595 if(args.size() != 0){
1596 vArgs.setNewObject(new SpTuple(args));
1599 vBody.setObject(body);
1602 func = new SpUsrFunc(NilObject, vArgs, NilObject, NilObject, vBody, NilObject, NilObject);
1604 return SpObjectResult(func);
1607 // init Message Handler
1608 void SpString::init()
1610 StringMsgHandler.append(SymIsString, PrimTrue);
1612 PrimAdd.setNewObject(new SpPrim2(_prim_add));
1614 SpValue PrimLength(new SpPrim1(str_length));
1615 StringMsgHandler.append(SymLength, PrimLength);
1617 SpValue PrimNth(new SpPrim2(prim_nth));
1618 StringMsgHandler.append(SymNth, PrimNth);
1620 SpValue SymSub(new SpSymbol("sub"));
1621 SpValue PrimSub(new SpPrim3(prim_sub));
1622 StringMsgHandler.append(SymSub, PrimSub);
1624 SpValue SymSymbol(new SpSymbol("symbol"));
1625 SpValue PrimSymbol(new SpPrim1(str2symbol));
1626 StringMsgHandler.append(SymSymbol, PrimSymbol);
1628 SpValue PrimSplit(new SpPrim2(prim_split));
1629 StringMsgHandler.append(SymSplit, PrimSplit);
1631 SpValue SymTr(new SpSymbol("tr"));
1632 SpValue PrimTr(new SpPrim3(prim_tr));
1633 StringMsgHandler.append(SymTr, PrimTr);
1635 SpValue PrimReader(new SpPrim1(prim_reader));
1636 StringMsgHandler.append(SymReader, PrimReader);
1638 SpValue PrimUpper(new SpPrim1(prim_upper));
1639 StringMsgHandler.append(SymUpper, PrimUpper);
1641 SpValue PrimLower(new SpPrim1(prim_lower));
1642 StringMsgHandler.append(SymLower, PrimLower);
1644 SpValue PrimReplace(new SpPrim3(prim_replace));
1645 StringMsgHandler.append(SymReplace, PrimReplace);
1647 SpValue PrimToInt(new SpPrim1(prim_toInt));
1648 StringMsgHandler.append(SymToInt, PrimToInt);
1650 SpValue PrimFind(new SpPrim3(prim_find));
1651 StringMsgHandler.append(SymFind, PrimFind);
1653 SpValue PrimSplit1(new SpPrim2(prim_split1));
1654 StringMsgHandler.append(SymSplit1, PrimSplit1);
1656 SpValue PrimReplace1(new SpPrim3(prim_replace1));
1657 StringMsgHandler.append(SymReplace1, PrimReplace1);
1659 SpValue SymFormat(new SpSymbol("format"));
1660 SpValue PrimFormat(new SpPrim1(prim_format));
1661 StringMsgHandler.append(SymFormat, PrimFormat);