Andy Hung | 1ea842e | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
Brian Lindahl | 778b15a | 2023-05-13 07:42:13 -0600 | [diff] [blame] | 18 | #define LOG_TAG "mediametrics::stringutils" |
Andy Hung | 1ea842e | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "StringUtils.h" |
Andy Hung | 76a772a | 2024-11-14 19:58:12 -0800 | [diff] [blame] | 22 | #include "AudioTypes.h" |
| 23 | #include <audio_utils/StringUtils.h> |
Brian Lindahl | 778b15a | 2023-05-13 07:42:13 -0600 | [diff] [blame] | 24 | #include <charconv> |
| 25 | |
Andy Hung | 1ea842e | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 26 | namespace android::mediametrics::stringutils { |
| 27 | |
Andy Hung | 1ea842e | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 28 | size_t replace(std::string &str, const char *targetChars, const char replaceChar) |
| 29 | { |
| 30 | size_t replaced = 0; |
| 31 | for (char &c : str) { |
| 32 | if (strchr(targetChars, c) != nullptr) { |
| 33 | c = replaceChar; |
| 34 | ++replaced; |
| 35 | } |
| 36 | } |
| 37 | return replaced; |
| 38 | } |
| 39 | |
Andy Hung | 76adef7 | 2022-09-14 17:24:19 -0700 | [diff] [blame] | 40 | template <types::AudioEnumCategory CATEGORY> |
| 41 | std::pair<std::string /* external statsd */, std::string /* internal */> |
| 42 | parseDevicePairs(const std::string& devicePairs) { |
| 43 | std::pair<std::string, std::string> result{}; |
Andy Hung | 76a772a | 2024-11-14 19:58:12 -0800 | [diff] [blame] | 44 | const auto devaddrvec = audio_utils::stringutils::getDeviceAddressPairs(devicePairs); |
Andy Hung | 76adef7 | 2022-09-14 17:24:19 -0700 | [diff] [blame] | 45 | for (const auto& [device, addr] : devaddrvec) { // addr ignored for now. |
| 46 | if (!result.second.empty()) { |
| 47 | result.second.append("|"); // delimit devices with '|'. |
| 48 | result.first.append("|"); |
| 49 | } |
| 50 | result.second.append(device); |
| 51 | result.first.append(types::lookup<CATEGORY, std::string>(device)); |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | std::pair<std::string /* external statsd */, std::string /* internal */> |
| 57 | parseOutputDevicePairs(const std::string& devicePairs) { |
| 58 | return parseDevicePairs<types::OUTPUT_DEVICE>(devicePairs); |
| 59 | } |
| 60 | |
| 61 | std::pair<std::string /* external statsd */, std::string /* internal */> |
| 62 | parseInputDevicePairs(const std::string& devicePairs) { |
| 63 | return parseDevicePairs<types::INPUT_DEVICE>(devicePairs); |
| 64 | } |
| 65 | |
Andy Hung | 1ea842e | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 66 | } // namespace android::mediametrics::stringutils |