1 /*
2 * Copyright (c) 2021 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 "tools.h"
17
18 #include <string>
19
20 namespace OHOS {
21 const std::string EMPTY_DEVICEID = "";
22 constexpr int NON_ANONYMIZE_LENGTH = 6;
23 // template<typename T>
DeleteAllMark(const std::u16string & str,const std::u16string & mark)24 std::u16string DeleteAllMark(const std::u16string& str, const std::u16string& mark)
25 {
26 std::u16string s = str;
27 size_t nSize = mark.size();
28 while (true) {
29 size_t pos = s.find(mark);
30 if (pos == std::string::npos) {
31 return s;
32 }
33 s.erase(pos, nSize);
34 }
35 return s;
36 }
37
38 // template<typename T>
DeleteBlank(const std::u16string & str)39 std::u16string DeleteBlank(const std::u16string& str)
40 {
41 std::u16string res = DeleteAllMark(str, u" ");
42 res = DeleteAllMark(res, u"/t");
43 res = DeleteAllMark(res, u"/n");
44 return DeleteAllMark(res, u"/r");
45 }
46
AnonymizeDeviceId(const std::string & deviceId)47 std::string AnonymizeDeviceId(const std::string& deviceId)
48 {
49 if (deviceId.length() < NON_ANONYMIZE_LENGTH) {
50 return EMPTY_DEVICEID;
51 }
52 std::string anonymizeDeviceId = deviceId.substr(0, NON_ANONYMIZE_LENGTH);
53 anonymizeDeviceId.append("******");
54 return anonymizeDeviceId;
55 }
56 }