/** * Object.prototype.toString() */ Stringp ObjectClass::_toString(Atom thisAtom) { AvmCore* core = this->core(); if (core->istype(thisAtom, CLASS_TYPE)) { ClassClosure *cc = (ClassClosure *)AvmCore::atomToScriptObject(thisAtom); Traits* t = cc->ivtable()->traits; Stringp s = core->concatStrings(core->newString("[class "), t->name); return core->concatStrings(s, core->newString("]")); } else { Traits* t = toplevel()->toTraits(thisAtom); Stringp s = core->concatStrings(core->newString("[object "), t->name); return core->concatStrings(s, core->newString("]")); } }
Stringp FileClass::read(Stringp filename) { Toplevel* toplevel = this->toplevel(); AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } UTF8String* filenameUTF8 = filename->toUTF8String(); FILE *fp = fopen(filenameUTF8->c_str(), "r"); if (fp == NULL) { toplevel->throwError(kFileOpenError, filename); } fseek(fp, 0L, SEEK_END); long len = ftell(fp); #ifdef UNDER_CE fseek (fp, 0L, SEEK_SET); #else rewind(fp); #endif unsigned char *c = new unsigned char[len+1]; len = (long)fread(c, 1, len, fp); c[len] = 0; fclose(fp); if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { return core->newString(((char *)c) + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; Stringp out = new (core->GetGC()) String(len); wchar *buffer = out->lockBuffer(); for (long i = 0; i < len; i++) { buffer[i] = (c[0] << 8) + c[1]; c += 2; } out->unlockBuffer(); return out; } else if ((c[0] == 0xff) && (c[1] == 0xfe))
ArrayObject* RegExpObject::split(Stringp subject, uint32 limit) { AvmCore *core = this->core(); ArrayObject *out = toplevel()->arrayClass->newArray(); UsesUTF8String utf8Subject(subject); int startIndex=0; int matchIndex; int matchLen; ArrayObject* matchArray; unsigned n=0; bool isEmptyRE = m_source->length() == 0; while ((matchArray = _exec(subject, utf8Subject, startIndex, matchIndex, matchLen)) != NULL) { // [cn 11/22/04] when match is made, but is length 0 we've matched the empty // position between characters. Although we've "matched", its zero length so just break out. if (matchLen == 0 ) { matchLen = 0; matchIndex = startIndex+numBytesInUtf8Character((uint8*)(utf8Subject->c_str())+startIndex); // +1char will advance startIndex, extract just one char if( !isEmptyRE ) { // don't break if we're processing an empty regex - then we want to split the string into each character // so we want the loop to continue break; } } //[ed 8/10/04] don't go past end of string. not sure why pcre doesn't return null //for a match starting past the end. //[cn 12/3/04] because a regular expression which matches an empty position (space between characters) // will match the empty position just past the last character. This test is correct, though // it needs to come before we do any setProperties to avoid a bogus xtra result. if (matchIndex+matchLen > utf8Subject->length()) { startIndex = matchIndex+matchLen; break; } else { out->setUintProperty(n++, (core->newString(utf8Subject->c_str()+startIndex, matchIndex-startIndex))->atom()); if (n >= limit) break; for (uint32 j=1; j<matchArray->getLength(); j++) { out->setUintProperty(n++, matchArray->getUintProperty(j)); if (n >= limit) break; } // Advance past this match startIndex = matchIndex+matchLen; } } // If we found no match, or we did find a match and are still under limit, and there is a remainder left, add it if ((unsigned)n < limit && startIndex <= utf8Subject->length()) { out->setUintProperty(n++, (core->newString(utf8Subject->c_str()+startIndex, utf8Subject->length()-startIndex))->atom()); } return out; }