blob: af5d10f8522bf47f29a7b7509a69a756043863f1 [file] [log] [blame]
Chris Craik6e6646c2015-09-14 15:54:12 -07001/*
2 * Copyright (C) 2015 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#ifndef STRING_UTILS_H
17#define STRING_UTILS_H
18
sergeyvc3849aa2016-08-08 13:22:06 -070019#include <iomanip>
20#include <iostream>
21#include <ostream>
22#include <sstream>
Chris Craik6e6646c2015-09-14 15:54:12 -070023#include <string>
24#include <unordered_set>
sergeyvc3849aa2016-08-08 13:22:06 -070025
26#include <utils/Log.h>
Chris Craik6e6646c2015-09-14 15:54:12 -070027
28namespace android {
29namespace uirenderer {
30
John Reck704bed02015-11-05 09:22:17 -080031class unordered_string_set : public std::unordered_set<std::string> {
Chris Craik6e6646c2015-09-14 15:54:12 -070032public:
John Reck704bed02015-11-05 09:22:17 -080033 bool has(const char* str) {
34 return find(std::string(str)) != end();
35 }
36};
37
38class StringUtils {
39public:
40 static unordered_string_set split(const char* spacedList);
Chris Craik6e6646c2015-09-14 15:54:12 -070041};
42
John Reck38e0c322015-11-10 12:19:17 -080043struct SizePrinter {
44 int bytes;
45 friend std::ostream& operator<<(std::ostream& stream, const SizePrinter& d) {
46 static const char* SUFFIXES[] = {"B", "KiB", "MiB"};
47 size_t suffix = 0;
48 double temp = d.bytes;
Chris Craik91eff222016-02-22 13:39:33 -080049 while (temp > 1024 && suffix < 2) {
John Reck38e0c322015-11-10 12:19:17 -080050 temp /= 1024.0;
51 suffix++;
52 }
53 stream << std::fixed << std::setprecision(2) << temp << SUFFIXES[suffix];
54 return stream;
55 }
56};
57
sergeyvc3849aa2016-08-08 13:22:06 -070058class LogcatStream: public std::ostream {
59 class LogcatStreamBuf: public std::stringbuf {
60 virtual int sync() {
61 ALOGD("%s", str().c_str());
62 str("");
63 return 0;
64 }
65 };
66
67 LogcatStreamBuf buffer;
68public:
69 LogcatStream()
70 :std::ostream(&buffer) {
71 }
72};
73
Chris Craik6e6646c2015-09-14 15:54:12 -070074} /* namespace uirenderer */
75} /* namespace android */
76
77#endif /* GLUTILS_H */