1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ecmascript/dfx/hprof/string_hashmap.h" 17 18 #include "ecmascript/mem/native_area_allocator.h" 19 20 namespace panda::ecmascript { FindOrInsertString(const CString * cstr)21CString *StringHashMap::FindOrInsertString(const CString *cstr) 22 { 23 StringKey key = GenerateStringKey(cstr); 24 auto it = hashmap_.find(key); 25 if (it != hashmap_.end()) { 26 return it->second; 27 } else { // NOLINT(readability-else-after-return) 28 index_++; 29 auto *newStr = const_cast<NativeAreaAllocator *>( 30 vm_->GetNativeAreaAllocator())->New<CString>(cstr->c_str()); 31 hashmap_.emplace(key, newStr); 32 orderedKey_.emplace_back(key); 33 indexMap_.emplace(key, index_); 34 return newStr; 35 } 36 } 37 GetStringId(const CString * cstr) const38StringId StringHashMap::GetStringId(const CString *cstr) const 39 { 40 auto it = indexMap_.find(GenerateStringKey(cstr)); 41 return it != indexMap_.end() ? it->second : 1; // "" 42 } 43 GetStringByKey(StringKey key) const44CString *StringHashMap::GetStringByKey(StringKey key) const 45 { 46 auto it = hashmap_.find(key); 47 if (it != hashmap_.end()) { 48 return FormatString(it->second); 49 } 50 return nullptr; 51 } 52 FormatString(CString * cstr) const53CString *StringHashMap::FormatString(CString *cstr) const 54 { 55 // remove "\"" | "\r\n" | "\\" | "\t" | "\f" 56 int length = static_cast<int>(cstr->length()); 57 char *charSeq = const_cast<char *>(cstr->c_str()); 58 for (int i = 0; i < length; i++) { 59 if (charSeq[i] == '\"') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 60 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 61 } else if (charSeq[i] == '\r') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 62 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 63 } else if (charSeq[i] == '\n') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 64 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 65 } else if (charSeq[i] == '\\') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 66 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 67 } else if (charSeq[i] == '\t') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 68 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 69 } else if (charSeq[i] == '\f') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 70 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 71 } else if (charSeq[i] < ' ') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 72 // ctrl chars 0~31 73 charSeq[i] = '`'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 74 } 75 } 76 *cstr = charSeq; 77 return cstr; 78 } 79 GenerateStringKey(const CString * cstr) const80StringKey StringHashMap::GenerateStringKey(const CString *cstr) const 81 { 82 return std::hash<CString>{} (*cstr); 83 } 84 GetString(const CString & cstr)85CString *StringHashMap::GetString(const CString &cstr) 86 { 87 return FindOrInsertString(&cstr); 88 } 89 Clear()90void StringHashMap::Clear() 91 { 92 auto *alloctor = const_cast<NativeAreaAllocator *>(vm_->GetNativeAreaAllocator()); 93 for (auto &[_, cstr] : hashmap_) { 94 if (cstr != nullptr) { 95 alloctor->Delete(cstr); 96 } 97 } 98 } 99 } // namespace panda::ecmascript 100