__ss_bool str::endswith(str *s, __ss_int start, __ss_int end) { __ss_int i, j, one = 1; slicenr(7, start, end, one, __len__()); for(i = end, j = len(s); i > start && j > 0; ) if (unit[--i] != s->unit[--j]) return False; return True; }
__ss_bool str::startswith(str *s, __ss_int start, __ss_int end) { __ss_int i, j, one = 1; slicenr(7, start, end, one, __len__()); for(i = start, j = 0; i < end && j < len(s); ) if (unit[i++] != s->unit[j++]) return False; return __mbool(j == len(s)); }
std::string PyArrayRef<T>::__repr__() const { std::stringstream ss; ss << "[ "; for (size_t i = 0; i < __len__(); ++i) ss << __getitem__(i) << " "; ss << "]"; return ss.str(); //return PyArrayBase<T, Array>::__repr__(); }
__ss_int str::count(str *s, __ss_int start, __ss_int end) { __ss_int i, count, one = 1; slicenr(7, start, end, one, __len__()); i = start; count = 0; while( ((i = unit.find(s->unit, i)) != -1) && (i <= end-len(s)) ) { i += len(s); count++; } return count; }
str *str::center(int w, str *fill) { int len = __len__(); if(w<=len) return this; if(!fill) fill = sp; str *r = fill->__mul__(w); int j = (w-len)/2; for(int i=0; i<len; i++) r->unit[j+i] = unit[i]; return r; }
str *str::swapcase() { str *r = new str(unit); int len = __len__(); for(int i = 0; i < len; i++) { char c = unit[i]; if( c >= 'a' && c <= 'z' ) r->unit[i] = ::toupper(c); else if( c >= 'A' && c <= 'Z' ) r->unit[i] = ::tolower(c); } return r; }
str *str::rjust(int width, str *s) { if(width<=__len__()) return this; if(!s) s = sp; return s->__mul__(width-__len__())->__add__(this); }
__ss_bool str::endswith(str *s, __ss_int start) { return endswith(s, start, __len__()); }
__ss_int str::count(str *s, __ss_int start) { return count(s, start, __len__()); }
long str::__hash__() { if(hash != -1) return hash; long x; const unsigned char *data = (unsigned char *)unit.data(); int len = __len__(); #ifdef __SS_FASTHASH //----------------------------------------------------------------------------- // MurmurHash2, by Austin Appleby // http://sites.google.com/site/murmurhash/ // All code is released to the public domain. // For business purposes, Murmurhash is under the MIT license. // Note - This code makes a few assumptions about how your machine behaves - // 1. We can read a 4-byte value from any address without crashing // 2. sizeof(int) == 4 // And it has a few limitations - // 1. It will not work incrementally. // 2. It will not produce the same results on little-endian and big-endian // machines. // 'm' and 'r' are mixing constants generated offline. // They're not really 'magic', they just happen to work well. unsigned int seed = 12345678; /* XXX */ const unsigned int m = 0x5bd1e995; const int r = 24; // Initialize the hash to a 'random' value x = seed ^ len; // Mix 4 bytes at a time into the hash while(len >= 4) { unsigned int k = *(unsigned int *)data; k *= m; k ^= k >> r; k *= m; x *= m; x ^= k; data += 4; len -= 4; } // Handle the last few bytes of the input array switch(len) { case 3: x ^= data[2] << 16; case 2: x ^= data[1] << 8; case 1: x ^= data[0]; x *= m; }; // Do a few final mixes of the hash to ensure the last few // bytes are well-incorporated. x ^= x >> 13; x *= m; x ^= x >> 15; #else /* modified from CPython */ x = *data << 7; while (--len >= 0) x = (1000003*x) ^ *data++; x ^= __len__(); if (x == -1) x = -2; #endif hash = x; return x; }
str *str::zfill(int width) { if(width<=__len__()) return this; return (new str("0"))->__mul__(width-__len__())->__add__(this); }
__ss_bool pyobj::__nonzero__() { return __mbool(__len__() != 0); }