void expectReplacementAt(const Replacement &Replace, StringRef File, unsigned Offset, unsigned Length) { ASSERT_TRUE(Replace.isApplicable()); EXPECT_EQ(File, Replace.getFilePath()); EXPECT_EQ(Offset, Replace.getOffset()); EXPECT_EQ(Length, Replace.getLength()); }
/* * Handles special case actor configuration (anything thats not already a property) * */ Actor SetupActor( const TreeNode& child, Actor& actor, const Replacement& constant ) { DALI_ASSERT_ALWAYS( actor && "Empty actor handle" ); // we allow enums strings for parent-origin and anchor-point but as with the current json // strings always succeed if they exist then check its not vector. If they are Vec3s then // this has already been set as a generic property. if( !IsVector3( child, "parent-origin") ) { if( OptionalVector3 v = constant.IsVector3( IsChild(child, "parent-origin") ) ) { actor.SetParentOrigin( *v ); } else if( OptionalString origin = constant.IsString( IsChild(child, "parent-origin") ) ) { actor.SetParentOrigin( GetAnchorConstant(*origin) ); } } if( !IsVector3(child, "anchor-point") ) { if( OptionalVector3 v = constant.IsVector3( IsChild(child, "anchor-point") ) ) { actor.SetAnchorPoint( *v ); } else if( OptionalString anchor = constant.IsString( IsChild(child, "anchor-point") ) ) { actor.SetAnchorPoint( GetAnchorConstant(*anchor) ); } } // Add custom properties if( OptionalChild customPropertiesChild = IsChild(child, "custom-properties") ) { const TreeNode& customPropertiesNode = *customPropertiesChild; const TreeConstIter endIter = customPropertiesNode.CEnd(); for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter ) { const TreeNode::KeyNodePair& keyChild = *iter; std::string key( keyChild.first ); Property::Index index = actor.GetPropertyIndex( key ); Property::Value value; if( SetPropertyFromNode( keyChild.second, value, constant )) { if( Property::INVALID_INDEX == index ) { actor.RegisterProperty( key, value, Property::READ_WRITE ); } else { actor.SetProperty( index, value ); } } } } return actor; }
int main() { string str = "he wo a"; /*string::size_type new_capacity{ 100u }; str.reserve(new_capacity);*/ //cout << str.size() << endl; cout << str << endl; Replacement rep; str = rep.replaceSpace(str, str.size()); cout << str << endl; }
Replacement* Replacements::addReplacement(string sRegex, string replacement) { Replacement* newReplacementPtr = new Replacement(mDb); newReplacementPtr->setRegex(sRegex); newReplacementPtr->setReplacement(replacement); //newReplacementPtr->setname(mname); stringstream strSql; strSql << "SELECT rowid FROM replacementGroups " << "WHERE name = " << cSqlStrOut(mName) << " AND ownerId = " << mOwnerId; string sRowId; exec(strSql, mDb, onReadFirstField, &sRowId); if (sRowId.length() > 0) { //sqlite_int64 rowId = cSqlInFormated<sqlite_int64>(sRowId); newReplacementPtr->setGroupId(cSqlInFormated<sqlite_int64>(sRowId)); } else { strSql.str(""); //clear strSql << "INSERT INTO replacementGroups " << "(name, ownerId) VALUES (" << cSqlStrOut(mName) << ", " << mOwnerId << ")"; exec(strSql, mDb); newReplacementPtr->setGroupId(sqlite3_last_insert_rowid(mDb)); } newReplacementPtr->save(); mChildren.insert(make_pair(newReplacementPtr->getRowId(), newReplacementPtr)); return newReplacementPtr; }
Replacements::Replacements(sqlite3* db, string name, sqlite_int64 ownerId) : mDb(db), mName(name), mOwnerId(ownerId) { exAssert(db!=NULL); exAssertDesc(ownerId > 0, "ownerId should be greater null"); sqlite_int64 groupId = 0; stringstream strSql; strSql << "SELECT rowid FROM replacementGroups " << "WHERE name = " << cSqlStrOut(mName) << " AND ownerId = " << mOwnerId; string sRowId; exec(strSql, mDb, onReadFirstField, &sRowId); if (sRowId.length() > 0) { groupId = cSqlInFormated<sqlite_int64>(sRowId); } else { strSql.str(""); //clear strSql << "INSERT INTO replacementGroups " << "(name, ownerId) VALUES (" << cSqlStrOut(mName) << ", " << mOwnerId << ")"; exec(strSql, mDb); groupId = sqlite3_last_insert_rowid(mDb); } vector<string> rowids; strSql.str(""); //clear strSql << "SELECT rowid FROM replacements " "WHERE groupId = " << cSqlOutFormated(groupId) ; exec(strSql, db, onAppendFirstColumnToVector, &rowids); for (vector<string>::iterator it = rowids.begin(); it != rowids.end(); it++) { stringstream strRowid(*it); sqlite_int64 rowid; strRowid >> rowid; Replacement* newReplacement = new Replacement(db, rowid); mChildren[newReplacement->getRowId()] = newReplacement; } };
bool operator==(const Replacement &LHS, const Replacement &RHS) { return LHS.getOffset() == RHS.getOffset() && LHS.getLength() == RHS.getLength() && LHS.getFilePath() == RHS.getFilePath() && LHS.getReplacementText() == RHS.getReplacementText(); }
bool operator<(const Replacement &LHS, const Replacement &RHS) { if (LHS.getOffset() != RHS.getOffset()) return LHS.getOffset() < RHS.getOffset(); if (LHS.getLength() != RHS.getLength()) return LHS.getLength() < RHS.getLength(); if (LHS.getFilePath() != RHS.getFilePath()) return LHS.getFilePath() < RHS.getFilePath(); return LHS.getReplacementText() < RHS.getReplacementText(); }
bool operator<(const Replacement &LHS, const Replacement &RHS) { if (LHS.getOffset() != RHS.getOffset()) return LHS.getOffset() < RHS.getOffset(); // Apply longer replacements first, specifically so that deletions are // executed before insertions. It is (hopefully) never the intention to // delete parts of newly inserted code. if (LHS.getLength() != RHS.getLength()) return LHS.getLength() > RHS.getLength(); if (LHS.getFilePath() != RHS.getFilePath()) return LHS.getFilePath() < RHS.getFilePath(); return LHS.getReplacementText() < RHS.getReplacementText(); }
bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacer ) { bool done = false; // some values are ambiguous as we have no Property::Type but can be disambiguated in the json // Currently Rotations and Rectangle must always be disambiguated when a type isnt available if( Disambiguated( node, value, replacer ) ) { done = true; } else { if( node.Size() ) { // our current heuristic for deciding an array is actually a vector and not say a map // is to check if the values are all floats bool allNumbers = true; for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter) { OptionalFloat f = IsFloat((*iter).second); if(!f) { allNumbers = false; break; } } if( allNumbers ) { // prefer finding vectors over presuming composite Property::Array... if( OptionalMatrix v = IsMatrix(node) ) { value = *v; done = true; } else if( OptionalMatrix3 v = IsMatrix3(node) ) { value = *v; done = true; } else if( OptionalVector4 v = IsVector4(node) ) { value = *v; done = true; } else if( OptionalVector3 v = IsVector3(node) ) { value = *v; done = true; } else if( OptionalVector2 v = IsVector2(node) ) { value = *v; done = true; } else if( 4 == node.Size() ) { if( OptionalVector4 v = IsVector4(node) ) { value = *v; done = true; } } else { value = Property::Value(Property::ARRAY); Property::Array* array = value.GetArray(); if( array ) { for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter) { Property::Value childValue; if( SetPropertyFromNode( (*iter).second, childValue, replacer ) ) { array->PushBack( childValue ); done = true; } } } } } if(!done) { // presume an array or map // container of size 1 TreeNode::ConstIterator iter = node.CBegin(); // its seems legal with current json parser for a map to have an empty key // but here we take that to mean the structure is a list if( ((*iter).first) == 0 ) { value = Property::Value(Property::ARRAY); Property::Array* array = value.GetArray(); if( array ) { for(unsigned int i = 0; i < node.Size(); ++i, ++iter) { Property::Value childValue; if( SetPropertyFromNode( (*iter).second, childValue, replacer ) ) { array->PushBack( childValue ); done = true; } } } } else { value = Property::Value(Property::MAP); Property::Map* map = value.GetMap(); if( map ) { for(unsigned int i = 0; i < node.Size(); ++i, ++iter) { Property::Value childValue; if( SetPropertyFromNode( (*iter).second, childValue, replacer ) ) { map->Insert( (*iter).first, childValue ); done = true; } } } } } // if!done } // if node.size() else // if( 0 == node.size() ) { // no children so either one of bool, float, integer, string OptionalBoolean aBool = replacer.IsBoolean(node); OptionalInteger anInt = replacer.IsInteger(node); OptionalFloat aFloat = replacer.IsFloat(node); OptionalString aString = replacer.IsString(node); if(aBool) { // a bool is also an int but here we presume int if(anInt) { value = *anInt; done = true; } else { value = *aBool; done = true; } } else { // Note: These are both floats and strings // {"value":"123"} // {"value":123} // This means we can't have a string with purely numeric content without disambiguation. if(aFloat) { value = *aFloat; done = true; } else if(anInt) { value = *anInt; done = true; } else { // string always succeeds with the current json parser so its last value = *aString; done = true; } } // if aBool } // if( node.size() ) } // if Disambiguated() return done; } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacer ) { bool done = false; switch(type) { case Property::BOOLEAN: { if( OptionalBoolean v = replacer.IsBoolean(node) ) { value = *v; done = true; } break; } case Property::FLOAT: { if( OptionalFloat v = replacer.IsFloat(node) ) { value = *v; done = true; } break; } case Property::INTEGER: { if( OptionalInteger v = replacer.IsInteger(node) ) { value = *v; done = true; } break; } case Property::VECTOR2: { if( OptionalVector2 v = replacer.IsVector2(node) ) { value = *v; done = true; } break; } case Property::VECTOR3: { if( OptionalVector3 v = replacer.IsVector3(node) ) { value = *v; done = true; } break; } case Property::VECTOR4: { if( OptionalVector4 v = replacer.IsVector4(node) ) { value = *v; done = true; } else if( OptionalString s = replacer.IsString(node) ) { if( (*s)[0] == '#' && 7 == (*s).size() ) { value = HexStringToVector4( &(*s)[1] ); done = true; } else if( Dali::ColorController::Get() ) { Vector4 color; done = Dali::ColorController::Get().RetrieveColor( *s, color ); value = color; } } else if( TreeNode::OBJECT == node.GetType() ) { // check for "r", "g" and "b" child color component nodes OptionalInteger r = replacer.IsInteger( IsChild(node, "r") ); OptionalInteger g = replacer.IsInteger( IsChild(node, "g") ); OptionalInteger b = replacer.IsInteger( IsChild(node, "b") ); if( r && g && b ) { float red( (*r) * (1.0f/255.0f) ); float green( (*g) * (1.0f/255.0f) ); float blue( (*b) * (1.0f/255.0f) ); // check for optional "a" (alpha) node, default to fully opaque if it is not found. float alpha( 1.0f ); OptionalInteger a = replacer.IsInteger( IsChild(node, "a") ); if( a ) { alpha = (*a) * (1.0f/255.0f); } value = Vector4( red, green, blue, alpha ); done = true; } } break; } case Property::MATRIX3: { if( OptionalMatrix3 v = replacer.IsMatrix3(node) ) { value = *v; done = true; } break; } case Property::MATRIX: { if( OptionalMatrix v = replacer.IsMatrix(node) ) { value = *v; done = true; } break; } case Property::RECTANGLE: { if( OptionalRect v = replacer.IsRect(node) ) { value = *v; done = true; } break; } case Property::ROTATION: { if(4 == node.Size()) { if( OptionalVector4 ov = replacer.IsVector4(node) ) { const Vector4& v = *ov; // angle, axis as per spec value = Quaternion(Radian(Degree(v[3])), Vector3(v[0],v[1],v[2])); done = true; } } else { // degrees Euler as per spec if( OptionalVector3 v = replacer.IsVector3(node) ) { value = Quaternion(Radian(Degree((*v).x)), Radian(Degree((*v).y)), Radian(Degree((*v).z))); done = true; } } break; } case Property::STRING: { if( OptionalString v = replacer.IsString(node) ) { value = *v; done = true; } break; } case Property::ARRAY: { if( replacer.IsArray( node, value ) ) { done = true; } else if(node.Size()) { value = Property::Value(Property::ARRAY); Property::Array* array = value.GetArray(); unsigned int i = 0; TreeNode::ConstIterator iter(node.CBegin()); if( array ) { for( ; i < node.Size(); ++i, ++iter) { Property::Value childValue; if( SetPropertyFromNode( (*iter).second, childValue, replacer ) ) { array->PushBack( childValue ); } } if( array->Count() == node.Size() ) { done = true; } else { done = false; } } } break; } case Property::MAP: { if( replacer.IsMap( node, value ) ) { done = true; } else if(node.Size()) { value = Property::Value(Property::MAP); Property::Map* map = value.GetMap(); unsigned int i = 0; TreeNode::ConstIterator iter(node.CBegin()); if( map ) { for( ; i < node.Size(); ++i, ++iter) { Property::Value childValue; if( SetPropertyFromNode( (*iter).second, childValue, replacer ) ) { map->Insert( (*iter).first, childValue ); } } if( map->Count() == node.Size() ) { done = true; } else { done = false; } } } break; } case Property::NONE: { break; } } // switch type return done; }