void DictionaryValue::Set(const std::string& path, Value* in_value) { DCHECK(IsStringUTF8(path)); DCHECK(in_value); std::string current_path(path); DictionaryValue* current_dictionary = this; for(size_t delimiter_position=current_path.find('.'); delimiter_position!=std::string::npos; delimiter_position=current_path.find('.')) { // 按路径进入字典中的索引. std::string key(current_path, 0, delimiter_position); DictionaryValue* child_dictionary = NULL; if(!current_dictionary->GetDictionary(key, &child_dictionary)) { child_dictionary = new DictionaryValue; current_dictionary->SetWithoutPathExpansion(key, child_dictionary); } current_dictionary = child_dictionary; current_path.erase(0, delimiter_position+1); } current_dictionary->SetWithoutPathExpansion(current_path, in_value); }
void SplitStringDontTrim(const std::string& str, char c, std::vector<std::string>* r) { DCHECK(IsStringUTF8(str)); DCHECK(c>=0 && c<0x7F); SplitStringT(str, c, false, r); }
bool DictionaryValue::HasKey(const std::string& key) const { DCHECK(IsStringUTF8(key)); ValueMap::const_iterator current_entry = dictionary_.find(key); DCHECK((current_entry == dictionary_.end()) || current_entry->second); return current_entry != dictionary_.end(); }
Value* JSONReader::JsonToValue(const std::string& json, bool check_root, bool allow_trailing_comma) { // 输入必须是UTF-8编码. if(!IsStringUTF8(json.c_str())) { error_code_ = JSON_UNSUPPORTED_ENCODING; return NULL; } // 从UTF8到wstring的转换会移除空字节(好事). std::wstring json_wide(UTF8ToWide(json)); start_pos_ = json_wide.c_str(); // 当输入的JSON字符串开头有UTF-8的Byte-Order-Mark(0xEF, 0xBB, 0xBF), // UTF8ToWide()函数会把它转换成BOM(U+FEFF). 为防止JSONReader::BuildValue() // 函数把它当成非法字符而返回NULL, 如果存在Unicode的BOM则先跳过. if(!json_wide.empty() && start_pos_[0]==0xFEFF) { ++start_pos_; } json_pos_ = start_pos_; allow_trailing_comma_ = allow_trailing_comma; stack_depth_ = 0; error_code_ = JSON_NO_ERROR; scoped_ptr<Value> root(BuildValue(check_root)); if(root.get()) { if(ParseToken().type == Token::END_OF_INPUT) { return root.release(); } else { SetErrorCode(JSON_UNEXPECTED_DATA_AFTER_ROOT, json_pos_); } } // "语法错误". if(error_code_ == 0) { SetErrorCode(JSON_SYNTAX_ERROR, json_pos_); } return NULL; }
bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, Value** out_value) const { DCHECK(IsStringUTF8(key)); ValueMap::const_iterator entry_iterator = dictionary_.find(key); if(entry_iterator == dictionary_.end()) { return false; } Value* entry = entry_iterator->second; if(out_value) { *out_value = entry; } return true; }
bool DictionaryValue::Remove(const std::string& path, Value** out_value) { DCHECK(IsStringUTF8(path)); std::string current_path(path); DictionaryValue* current_dictionary = this; size_t delimiter_position = current_path.rfind('.'); if(delimiter_position != std::string::npos) { if(!GetDictionary(current_path.substr(0, delimiter_position), ¤t_dictionary)) { return false; } current_path.erase(0, delimiter_position + 1); } return current_dictionary->RemoveWithoutPathExpansion(current_path, out_value); }
bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key, Value** out_value) { DCHECK(IsStringUTF8(key)); ValueMap::iterator entry_iterator = dictionary_.find(key); if(entry_iterator == dictionary_.end()) { return false; } Value* entry = entry_iterator->second; if(out_value) { *out_value = entry; } else { delete entry; } dictionary_.erase(entry_iterator); return true; }
bool DictionaryValue::Get(const std::string& path, Value** out_value) const { DCHECK(IsStringUTF8(path)); std::string current_path(path); const DictionaryValue* current_dictionary = this; for(size_t delimiter_position=current_path.find('.'); delimiter_position!=std::string::npos; delimiter_position=current_path.find('.')) { DictionaryValue* child_dictionary = NULL; if(!current_dictionary->GetDictionary(current_path.substr( 0, delimiter_position), &child_dictionary)) { return false; } current_dictionary = child_dictionary; current_path.erase(0, delimiter_position+1); } return current_dictionary->GetWithoutPathExpansion(current_path, out_value); }
void CommandLine::AppendArg(const std::string& value) { DCHECK(IsStringUTF8(value)); AppendArgNative(UTF8ToWide(value)); }
StringValue::StringValue(const std::string& in_value) : Value(TYPE_STRING), value_(in_value) { DCHECK(IsStringUTF8(in_value)); }