FBCALL FB_WCHAR *fb_WstrTrim ( const FB_WCHAR *src ) { FB_WCHAR *dst; const FB_WCHAR *p; ssize_t chars; if( src == NULL ) return NULL; chars = fb_wstr_Len( src ); if( chars <= 0 ) return NULL; p = fb_wstr_SkipCharRev( src, chars, _LC(' ') ); chars = fb_wstr_CalcDiff( src, p ); if( chars <= 0 ) return NULL; p = fb_wstr_SkipChar( src, chars, _LC(' ') ); chars -= fb_wstr_CalcDiff( src, p ); if( chars <= 0 ) return NULL; /* alloc temp string */ dst = fb_wstr_AllocTemp( chars ); if( dst != NULL ) { /* simple copy */ fb_wstr_Copy( dst, p, chars ); } return dst; }
/*:::::*/ FBCALL int fb_WstrToInt( const FB_WCHAR *src, int len ) { const FB_WCHAR *p, *r; int radix; /* skip white spc */ p = fb_wstr_SkipChar( src, len, 32 ); len -= fb_wstr_CalcDiff( src, p ); if( len < 1 ) return 0; radix = 10; r = p; if( (len >= 2) && (*r++ == L'&') ) { switch( *r++ ) { case L'h': case L'H': radix = 16; break; case L'o': case L'O': radix = 8; break; case L'b': case L'B': radix = 2; break; default: /* assume octal */ radix = 8; r--; break; } if( radix != 10 ) p = r; } /* wcstol() saturates values outside [-2^31, 2^31) so use wcstoul() instead */ return (int)wcstoul( p, NULL, radix ); }
FBCALL FB_WCHAR *fb_WstrTrimEx ( const FB_WCHAR *src, const FB_WCHAR *pattern ) { FB_WCHAR *dst; ssize_t len; const FB_WCHAR *p = src; if( src == NULL ) { return NULL; } { ssize_t len_pattern = fb_wstr_Len( pattern ); len = fb_wstr_Len( src ); if( len >= len_pattern ) { if( len_pattern==1 ) { p = fb_wstr_SkipChar( src, len, *pattern ); len -= fb_wstr_CalcDiff( src, p ); } else if( len_pattern != 0 ) { p = src; while (len >= len_pattern ) { if( fb_wstr_Compare( p, pattern, len_pattern )!=0 ) break; p += len_pattern; len -= len_pattern; } } } if( len >= len_pattern ) { if( len_pattern==1 ) { const FB_WCHAR *p_tmp = fb_wstr_SkipCharRev( p, len, *pattern ); len = fb_wstr_CalcDiff( p, p_tmp ); } else if( len_pattern != 0 ) { ssize_t test_index = len - len_pattern; while (len >= len_pattern ) { if( fb_wstr_Compare( p + test_index, pattern, len_pattern )!=0 ) break; test_index -= len_pattern; } len = test_index + len_pattern; } } } if( len > 0 ) { /* alloc temp string */ dst = fb_wstr_AllocTemp( len ); if( dst != NULL ) { /* simple copy */ fb_wstr_Copy( dst, p, len ); } else dst = NULL; } else dst = NULL; return dst; }
FBCALL double fb_WstrToDouble( const FB_WCHAR *src, ssize_t len ) { const FB_WCHAR *p, *r; int radix; ssize_t i; FB_WCHAR *q, c; double ret; /* skip white spc */ p = fb_wstr_SkipChar( src, len, 32 ); len -= fb_wstr_CalcDiff( src, p ); if( len < 1 ) return 0.0; r = p; if( (len >= 2) && (*r++ == L'&') ) { radix = 0; switch( *r++ ) { case L'h': case L'H': radix = 16; break; case L'o': case L'O': radix = 8; break; case L'b': case L'B': radix = 2; break; default: /* assume octal */ radix = 8; r--; break; } if( radix != 0 ) return (double)fb_WstrRadix2Longint( r, len - fb_wstr_CalcDiff( p, r ), radix ); } /* Workaround: wcstod() does not allow 'd' as an exponent specifier on * non-win32 platforms, so create a temporary buffer and replace any * 'd's with 'e' */ q = malloc( (len + 1) * sizeof(FB_WCHAR) ); for( i = 0; i < len; i++ ) { c = p[i]; if( c == L'd' || c == L'D' ) ++c; q[i]= c; } q[len] = L'\0'; ret = wcstod( q, NULL ); free( q ); return ret; }