20 #include "../../bscript/berror.h" 21 #include "../../bscript/bobject.h" 22 #include "../../bscript/dict.h" 23 #include "../../bscript/executor.h" 24 #include "../../bscript/impstr.h" 25 #include "../../clib/stlutil.h" 31 using namespace Bscript;
34 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 35 "abcdefghijklmnopqrstuvwxyz" 41 return ( isalnum( c ) || ( c ==
'+' ) || ( c ==
'/' ) );
55 return new BLong( static_cast<int>( arr->
ref_arr.size() ) );
68 return new BLong( 0 );
79 int posn = str->
find( d ? ( d - 1 ) : 0, s ) + 1;
81 return new BLong( posn );
91 return str->
StrStr( start, length );
99 return new BError(
"Param 1 must be a string." );
109 return string->ETrim( cset, type );
123 return new BError(
"Invalid parameter type" );
127 return new BError(
"Invalid parameter type" );
129 if ( string->length() < 1 )
130 return new BError(
"Cannot use empty string for string haystack." );
131 if ( to_replace->
length() < 1 )
132 return new BError(
"Cannot use empty string for string needle." );
134 string->EStrReplace( to_replace, replace_with );
147 return new BError(
"Invalid parameter type" );
152 return new BError(
"Index must not be negative" );
153 if ( static_cast<unsigned>( index - 1 ) > string->length() )
154 return new BError(
"Index out of range" );
160 if ( static_cast<unsigned>( len ) > (
string->length() - index ) )
161 return new BError(
"Length out of range" );
163 return new BError(
"Length must not be negative" );
166 len =
static_cast<int>( replace_with->
length() - index );
168 string->ESubStrReplace( replace_with, static_cast<unsigned>( index ),
169 static_cast<unsigned>( len ) );
171 return string.release();
185 if ( pos1_index != 0 )
187 if ( pos1_index < 0 )
188 return new BError(
"Index must not be negative for param 1" );
189 if ( static_cast<unsigned>( pos1_index - 1 ) > str1.length() )
190 return new BError(
"Index out of range for param 1" );
192 if ( pos2_index != 0 )
194 if ( pos2_index < 0 )
195 return new BError(
"Index must not be negative for param 2" );
196 if ( static_cast<unsigned>( pos2_index - 1 ) > str2.length() )
197 return new BError(
"Index out of range for param 2" );
202 return new BError(
"Length must not be negative for param 1" );
203 if ( static_cast<unsigned>( pos1_len ) > ( str1.length() - pos1_index ) )
204 return new BError(
"Length out of range for param 1" );
206 return new BError(
"Length must not be negative for param 2" );
207 if ( static_cast<unsigned>( pos2_len ) > ( str2.length() - pos2_index ) )
208 return new BError(
"Length out of range for param 2" );
211 if ( pos1_index == 0 )
213 unsigned int result = str1.compare( str2 );
215 return new BLong( 0 );
217 return new BLong( 1 );
219 else if ( pos1_index > 0 && pos2_index == 0 )
221 unsigned int result = str1.compare( pos1_index - 1, pos1_len, str2 );
223 return new BLong( 0 );
225 return new BLong( 1 );
229 unsigned int result = str1.compare( pos1_index - 1, pos1_len, str2, pos2_index - 1, pos2_len );
231 return new BLong( 0 );
233 return new BLong( 1 );
261 return new BLong( strtoul( str->
data(),
nullptr, 0 ) );
266 return new BLong( static_cast<int>( dbl->
value() ) );
270 return new BLong( 0 );
285 return new Double( strtod( str->
data(), nullptr ) );
309 return new BLong( static_cast<unsigned char>( str->
data()[0] ) );
313 return new BError(
"Invalid parameter type" );
323 for (
size_t i = 0; i < tmp.size(); ++i )
325 arr->addElement(
new BLong( static_cast<unsigned char>( tmp[i] ) ) );
328 arr->addElement(
new BLong( 0 ) );
330 return arr.release();
339 s[0] =
static_cast<char>( val );
345 return new BError(
"Invalid parameter type" );
356 return new BError(
"Invalid parameter type" );
358 itr != itrend; ++itr )
369 if ( break_first_null && blong->
value() == 0 )
372 s[0] =
static_cast<char>( blong->
value() );
388 sprintf( s,
"0x%X", static_cast<unsigned int>( plong->
value() ) );
395 sprintf( s,
"0x%X", static_cast<unsigned int>( pdbl->
value() ) );
402 sprintf( s,
"0x%X", static_cast<unsigned int>( strtoul( str->
data(),
nullptr, 0 ) ) );
407 return new BError(
"Hex() expects an Integer, Real, or String" );
412 char* itoa(
int value,
char* result,
int base )
415 if ( base < 2 || base > 16 )
422 int quotient = value;
426 *out =
"0123456789abcdef"[std::abs( quotient % base )];
429 }
while ( quotient );
432 if ( value < 0 && base == 10 )
435 std::reverse( result, out );
447 int number = plong->
value();
448 char buffer[
sizeof( int ) * 8 + 1];
453 return new BError(
"Bin() expects an Integer" );
462 const String* bimp_delimiter;
465 return new BError(
"Invalid parameter type." );
482 if ( delimiter ==
" " )
486 std::streamoff tellg = -1;
487 bool splitted =
false;
492 if ( count == max_split && tellg != -1 )
497 objarr->addElement(
new String( tmp ) );
505 std::string remaining_string;
506 remaining_string = source.substr( tellg - tmp.length(), source.length() );
507 objarr->addElement(
new String( remaining_string ) );
510 return objarr.release();
514 std::string new_string = source;
515 std::string::size_type found;
518 found = new_string.find( delimiter, 0 );
519 if ( found == std::string::npos )
521 else if ( count == max_split )
526 std::string add_string = new_string.substr( 0, found );
532 objarr->addElement(
new String( add_string ) );
533 std::string tmp_string = new_string.substr( found + delimiter.length(), new_string.length() );
534 new_string = tmp_string;
536 }
while ( found != std::string::npos );
539 if ( !new_string.empty() )
540 objarr->addElement(
new String( new_string ) );
542 return objarr.release();
561 return new BError(
"Invalid parameter type" );
588 int intVal =
static_cast<BLong*
>( v )->value();
589 return picojson::value( static_cast<double>( intVal ) );
593 return picojson::value( static_cast<Double*>( v )->value() );
597 return picojson::value( static_cast<BBoolean*>( v )->value() );
602 picojson::array jsonArr;
604 for (
const auto& elem : arr->
ref_arr )
612 return picojson::value( jsonArr );
617 picojson::object jsonObj;
618 for (
const auto& content : bstruct->
contents() )
621 jsonObj.insert( std::pair<std::string, picojson::value>( content.first,
recurseE2J( imp ) ) );
623 return picojson::value( jsonObj );
628 picojson::object jsonObj;
629 for (
const auto& content : cpropdict->
contents() )
632 jsonObj.insert( std::pair<std::string, picojson::value>( content.first->getStringRep(),
635 return picojson::value( jsonObj );
637 return picojson::value();
648 if ( v.is<std::string>() )
650 return new String( v.get<std::string>() );
652 else if ( v.is<
double>() )
655 return new Double( v.get<
double>() );
657 else if ( v.is<
bool>() )
659 return new BBoolean( v.get<
bool>() );
661 else if ( v.is<picojson::array>() )
663 std::unique_ptr<ObjArray> objarr(
new ObjArray );
664 const picojson::array& arr = v.get<picojson::array>();
665 for (
const auto& elem : arr )
669 return objarr.release();
671 else if ( v.is<picojson::object>() )
673 std::unique_ptr<BStruct> objstruct(
new BStruct );
674 for (
const auto& content : v.get<picojson::object>() )
676 objstruct->addMember( content.first.c_str(),
recurseJ2E( content.second ) );
678 return objstruct.release();
691 std::string err = picojson::parse( v, str->
data() );
700 return new BError(
"Invalid parameter type" );
714 return new BBoolean( *static_cast<BBoolean*>( imp ) );
718 return new BError(
"Boolean() expects an Integer or Boolean" );
730 if ( encoded_string.empty() )
731 return new BError(
"Invalid parameter type" );
733 unsigned int in_len =
static_cast<unsigned int>( encoded_string.length() );
735 unsigned char const* bytes_to_encode =
736 reinterpret_cast<const unsigned char*
>( encoded_string.c_str() );
740 unsigned char char_array_3[3];
741 unsigned char char_array_4[4];
745 char_array_3[i++] = *( bytes_to_encode++ );
748 char_array_4[0] = ( char_array_3[0] & 0xfc ) >> 2;
749 char_array_4[1] = ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 );
750 char_array_4[2] = ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 );
751 char_array_4[3] = char_array_3[2] & 0x3f;
753 for ( i = 0; ( i < 4 ); i++ )
754 ret += base64_chars[char_array_4[i]];
761 for ( j = i; j < 3; j++ )
762 char_array_3[j] =
'\0';
764 char_array_4[0] = ( char_array_3[0] & 0xfc ) >> 2;
765 char_array_4[1] = ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 );
766 char_array_4[2] = ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 );
767 char_array_4[3] = char_array_3[2] & 0x3f;
769 for ( j = 0; ( j < i + 1 ); j++ )
770 ret += base64_chars[char_array_4[j]];
772 while ( ( i++ < 3 ) )
783 if ( encoded_string.empty() )
784 return new BError(
"Invalid parameter type" );
786 int in_len =
static_cast<int>( encoded_string.size() );
790 unsigned char char_array_4[4], char_array_3[3];
793 while ( in_len-- && ( encoded_string[in_] !=
'=' ) &&
is_base64( encoded_string[in_] ) )
795 char_array_4[i++] = encoded_string[in_];
799 for ( i = 0; i < 4; i++ )
800 char_array_4[i] = static_cast<u8>( base64_chars.find( char_array_4[i] ) );
802 char_array_3[0] = ( char_array_4[0] << 2 ) + ( ( char_array_4[1] & 0x30 ) >> 4 );
803 char_array_3[1] = ( ( char_array_4[1] & 0xf ) << 4 ) + ( ( char_array_4[2] & 0x3c ) >> 2 );
804 char_array_3[2] = ( ( char_array_4[2] & 0x3 ) << 6 ) + char_array_4[3];
806 for ( i = 0; ( i < 3 ); i++ )
807 ret += char_array_3[i];
814 for ( j = i; j < 4; j++ )
817 for ( j = 0; j < 4; j++ )
818 char_array_4[j] = static_cast<u8>( base64_chars.find( char_array_4[j] ) );
820 char_array_3[0] = ( char_array_4[0] << 2 ) + ( ( char_array_4[1] & 0x30 ) >> 4 );
821 char_array_3[1] = ( ( char_array_4[1] & 0xf ) << 4 ) + ( ( char_array_4[2] & 0x3c ) >> 2 );
822 char_array_3[2] = ( ( char_array_4[2] & 0x3 ) << 6 ) + char_array_4[3];
824 for ( j = 0; ( j < i - 1 ); j++ )
825 ret += char_array_3[j];
834 using namespace Module;
Bscript::BObjectImp * mf_Trim()
virtual std::string getStringRep() const =0
Bscript::BObjectImp * mf_Compare()
static bool is_base64(unsigned char c)
Bscript::BObjectImp * lower()
int find(int begin, const char *target)
bool isa(BObjectType type) const
Bscript::BObjectImp * mf_TypeOfInt()
Bscript::BObjectImp * len()
Bscript::BObjectImp * mf_SplitWords()
bool getParam(unsigned param, int &value)
Bscript::BObjectImp * mf_Boolean()
Bscript::BObjectImp * mf_StrReplace()
virtual std::string getStringRep() const POL_OVERRIDE
const Contents & contents() const
Bscript::BObjectImp * upper()
int makeString(unsigned param)
virtual BObjectImp * copy() const =0
virtual u8 typeOfInt() const
Bscript::BObjectImp * mf_PackJSON()
int paramAsLong(unsigned param)
Bscript::BObjectImp * recurseJ2E(const picojson::value &v)
const char * paramAsString(unsigned param)
Bscript::BObjectImp * mf_TypeOf()
virtual std::string pack() const
Bscript::BObjectImp * mf_EncodeBase64()
Cont::const_iterator const_iterator
picojson::value recurseE2J(BObjectImp *v)
Bscript::BObjectImp * mf_CStr()
Bscript::BObjectImp * mf_Pack()
static const std::string base64_chars
static UninitObject * create()
BasicExecutorModule(Bscript::Executor &exec)
static BObjectImp * unpack(const char *pstr)
Bscript::BObjectImp * find()
Bscript::BObjectImp * mf_CAscZ()
Bscript::BObjectImp * mf_SubStrReplace()
String * StrStr(int begin, int len)
Bscript::BObjectImp * mf_CDbl()
Bscript::BObjectImp * mf_UnpackJSON()
Bscript::BObjectImp * mf_CChrZ()
const Contents & contents() const
const String * getStringParam(unsigned param)
Bscript::BObjectImp * mf_DecodeBase64()
Bscript::BObjectImp * mf_CChr()
Bscript::BObjectImp * mf_substr()
bool hasParams(unsigned howmany) const
virtual size_t sizeEstimate() const =0
Bscript::BObjectImp * mf_Unpack()
const char * data() const
Bscript::BObjectImp * mf_Bin()
Bscript::BObjectImp * mf_CInt()
Bscript::BObjectImp * mf_SizeOf()
virtual const char * typeOf() const
Bscript::BObjectImp * mf_CAsc()
Bscript::BObjectImp * mf_Hex()
BObjectImp * getParamImp(unsigned param)