forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeParser.cpp
More file actions
90 lines (80 loc) · 2.49 KB
/
ShapeParser.cpp
File metadata and controls
90 lines (80 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ShapeParser.h"
#include <Dim.h>
#include <algorithm>
#include <stdexcept>
#include <sstream>
using namespace circle_resizer;
namespace
{
bool is_blank(const std::string &s)
{
return !s.empty() && std::find_if(s.begin(), s.end(),
[](unsigned char c) { return !std::isblank(c); }) == s.end();
}
Shape parse_single_shape(const std::string &shape)
{
if (shape.empty() || is_blank(shape))
{
return Shape::scalar();
}
std::vector<Dim> result_dims;
std::stringstream shape_stream(shape);
std::string token;
try
{
while (std::getline(shape_stream, token, ','))
{
result_dims.push_back(Dim{std::stoi(token)});
}
}
catch (...)
{
throw std::invalid_argument("Error during shape processing: " + shape);
}
if (result_dims.empty())
{
throw std::invalid_argument("No shapes found in input string: " + shape);
}
return Shape{result_dims};
}
} // namespace
std::vector<Shape> circle_resizer::parse_shapes(const std::string &shapes)
{
std::vector<Shape> result_shapes;
auto shapes_tmp = shapes;
std::string token;
size_t begin_pos = 0, end_pos = 0;
while ((begin_pos = shapes_tmp.find_first_of("[")) != std::string::npos &&
(end_pos = shapes_tmp.find_first_of("]")) != std::string::npos)
{
const size_t token_size = end_pos - begin_pos - 1;
token = shapes_tmp.substr(begin_pos + 1, token_size);
result_shapes.push_back(parse_single_shape(token));
shapes_tmp.erase(0, end_pos + 1);
}
if (result_shapes.empty())
{
throw std::invalid_argument("No shapes found in the input string: " + shapes);
}
// the rest of the input not handled by loop above cannot be processed properly
if (shapes_tmp.size() > 0 && !is_blank(shapes_tmp))
{
throw std::invalid_argument("The part of input shapes: " + shapes_tmp + " cannot be processed");
}
return result_shapes;
}