blob: c4111ae73ff1f0fad2fc24d749c2be8dfa5d83c1 [file] [log] [blame]
Andy Hung1ea842e2020-05-18 10:47:31 -07001/*
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 Lindahl778b15a2023-05-13 07:42:13 -060018#define LOG_TAG "mediametrics::stringutils"
Andy Hung1ea842e2020-05-18 10:47:31 -070019#include <utils/Log.h>
20
21#include "StringUtils.h"
Andy Hung76a772a2024-11-14 19:58:12 -080022#include "AudioTypes.h"
23#include <audio_utils/StringUtils.h>
Brian Lindahl778b15a2023-05-13 07:42:13 -060024#include <charconv>
25
Andy Hung1ea842e2020-05-18 10:47:31 -070026namespace android::mediametrics::stringutils {
27
Andy Hung1ea842e2020-05-18 10:47:31 -070028size_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 Hung76adef72022-09-14 17:24:19 -070040template <types::AudioEnumCategory CATEGORY>
41std::pair<std::string /* external statsd */, std::string /* internal */>
42parseDevicePairs(const std::string& devicePairs) {
43 std::pair<std::string, std::string> result{};
Andy Hung76a772a2024-11-14 19:58:12 -080044 const auto devaddrvec = audio_utils::stringutils::getDeviceAddressPairs(devicePairs);
Andy Hung76adef72022-09-14 17:24:19 -070045 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
56std::pair<std::string /* external statsd */, std::string /* internal */>
57parseOutputDevicePairs(const std::string& devicePairs) {
58 return parseDevicePairs<types::OUTPUT_DEVICE>(devicePairs);
59}
60
61std::pair<std::string /* external statsd */, std::string /* internal */>
62parseInputDevicePairs(const std::string& devicePairs) {
63 return parseDevicePairs<types::INPUT_DEVICE>(devicePairs);
64}
65
Andy Hung1ea842e2020-05-18 10:47:31 -070066} // namespace android::mediametrics::stringutils