static void staticArrayStreamer(ArrayData* ad, std::stringstream& out) { out << "array("; if (!ad->empty()) { bool comma = false; for (ArrayIter it(ad); !it.end(); it.next()) { if (comma) { out << ","; } else { comma = true; } Variant key = it.first(); // Key. switch (key.getType()) { case KindOfInt64: { out << *key.getInt64Data(); break; } case KindOfStaticString: case KindOfString: { out << "\"" << Util::escapeStringForCPP(key.getStringData()->data(), key.getStringData()->size()) << "\""; break; } default: assert(false); } out << "=>"; // Value. Variant val = it.second(); switch (val.getType()) { case KindOfUninit: case KindOfNull: { out << "null"; break; } case KindOfBoolean: { out << (val.toBoolean() ? "true" : "false"); break; } case KindOfStaticString: case KindOfString: { out << "\"" << Util::escapeStringForCPP(val.getStringData()->data(), val.getStringData()->size()) << "\""; break; } case KindOfInt64: { out << *val.getInt64Data(); break; } case KindOfDouble: { out << *val.getDoubleData(); break; } case KindOfArray: { staticArrayStreamer(val.getArrayData(), out); break; } default: assert(false); } } } out << ")"; }
void staticArrayStreamer(const ArrayData* ad, std::ostream& out) { if (ad->isVecArray()) out << "vec("; else if (ad->isDict()) out << "dict("; else if (ad->isKeyset()) out << "keyset("; else { assert(ad->isPHPArray()); out << "array("; } if (!ad->empty()) { bool comma = false; for (ArrayIter it(ad); !it.end(); it.next()) { if (comma) { out << ","; } else { comma = true; } Variant key = it.first(); if (!ad->isVecArray() && !ad->isKeyset()) { // Key. if (isIntType(key.getType())) { out << *key.getInt64Data(); } else if (isStringType(key.getType())) { out << "\"" << escapeStringForCPP(key.getStringData()->data(), key.getStringData()->size()) << "\""; } else { assert(false); } out << "=>"; } Variant val = it.second(); // Value. [&] { switch (val.getType()) { case KindOfUninit: case KindOfNull: out << "null"; return; case KindOfBoolean: out << (val.toBoolean() ? "true" : "false"); return; case KindOfInt64: out << *val.getInt64Data(); return; case KindOfDouble: out << *val.getDoubleData(); return; case KindOfPersistentString: case KindOfString: out << "\"" << escapeStringForCPP(val.getStringData()->data(), val.getStringData()->size()) << "\""; return; case KindOfPersistentVec: case KindOfVec: case KindOfPersistentDict: case KindOfDict: case KindOfPersistentKeyset: case KindOfKeyset: case KindOfPersistentArray: case KindOfArray: staticArrayStreamer(val.getArrayData(), out); return; case KindOfObject: case KindOfResource: case KindOfRef: case KindOfClass: not_reached(); } }(); } } out << ")"; }
std::string array_string(SArray arr) { std::ostringstream str; staticArrayStreamer(const_cast<ArrayData*>(arr), str); return str.str(); }
void staticArrayStreamer(ArrayData* ad, std::ostream& out) { out << "array("; if (!ad->empty()) { bool comma = false; for (ArrayIter it(ad); !it.end(); it.next()) { if (comma) { out << ","; } else { comma = true; } Variant key = it.first(); // Key. if (IS_INT_TYPE(key.getType())) { out << *key.getInt64Data(); } else if (IS_STRING_TYPE(key.getType())) { out << "\"" << escapeStringForCPP(key.getStringData()->data(), key.getStringData()->size()) << "\""; } else { assert(false); } out << "=>"; Variant val = it.second(); // Value. [&] { switch (val.getType()) { case KindOfUninit: case KindOfNull: out << "null"; return; case KindOfBoolean: out << (val.toBoolean() ? "true" : "false"); return; case KindOfInt64: out << *val.getInt64Data(); return; case KindOfDouble: out << *val.getDoubleData(); return; case KindOfStaticString: case KindOfString: out << "\"" << escapeStringForCPP(val.getStringData()->data(), val.getStringData()->size()) << "\""; return; case KindOfArray: staticArrayStreamer(val.getArrayData(), out); return; case KindOfObject: case KindOfResource: case KindOfRef: case KindOfClass: not_reached(); } }(); } } out << ")"; }