1*3828e951SShawn McCarney /**
2*3828e951SShawn McCarney  * Copyright © 2024 IBM Corporation
3*3828e951SShawn McCarney  *
4*3828e951SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5*3828e951SShawn McCarney  * you may not use this file except in compliance with the License.
6*3828e951SShawn McCarney  * You may obtain a copy of the License at
7*3828e951SShawn McCarney  *
8*3828e951SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9*3828e951SShawn McCarney  *
10*3828e951SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11*3828e951SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12*3828e951SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3828e951SShawn McCarney  * See the License for the specific language governing permissions and
14*3828e951SShawn McCarney  * limitations under the License.
15*3828e951SShawn McCarney  */
16*3828e951SShawn McCarney #pragma once
17*3828e951SShawn McCarney 
18*3828e951SShawn McCarney #include <format>
19*3828e951SShawn McCarney #include <iterator>
20*3828e951SShawn McCarney #include <span>
21*3828e951SShawn McCarney #include <string>
22*3828e951SShawn McCarney 
23*3828e951SShawn McCarney /**
24*3828e951SShawn McCarney  * @namespace format_utils
25*3828e951SShawn McCarney  *
26*3828e951SShawn McCarney  * Contains utility functions for formatting data.
27*3828e951SShawn McCarney  */
28*3828e951SShawn McCarney namespace phosphor::power::format_utils
29*3828e951SShawn McCarney {
30*3828e951SShawn McCarney 
31*3828e951SShawn McCarney /**
32*3828e951SShawn McCarney  * Returns a string containing the elements in the specified container span.
33*3828e951SShawn McCarney  *
34*3828e951SShawn McCarney  * The string starts with "[", ends with "]", and the elements are separated by
35*3828e951SShawn McCarney  * ", ".  The individual elements are formatted using std::format.
36*3828e951SShawn McCarney  *
37*3828e951SShawn McCarney  * This function is a temporary solution until g++ implements the C++23
38*3828e951SShawn McCarney  * std::format() support for formatting ranges.
39*3828e951SShawn McCarney  *
40*3828e951SShawn McCarney  * @param span span of elements to format within a container
41*3828e951SShawn McCarney  * @return formatted string containing the specified elements
42*3828e951SShawn McCarney  */
43*3828e951SShawn McCarney template <typename T>
toString(const std::span<T> & span)44*3828e951SShawn McCarney std::string toString(const std::span<T>& span)
45*3828e951SShawn McCarney {
46*3828e951SShawn McCarney     std::string str{"["};
47*3828e951SShawn McCarney     for (auto it = span.cbegin(); it != span.cend(); ++it)
48*3828e951SShawn McCarney     {
49*3828e951SShawn McCarney         str += std::format("{}", *it);
50*3828e951SShawn McCarney         if (std::distance(it, span.cend()) > 1)
51*3828e951SShawn McCarney         {
52*3828e951SShawn McCarney             str += ", ";
53*3828e951SShawn McCarney         }
54*3828e951SShawn McCarney     }
55*3828e951SShawn McCarney     str += "]";
56*3828e951SShawn McCarney     return str;
57*3828e951SShawn McCarney }
58*3828e951SShawn McCarney 
59*3828e951SShawn McCarney } // namespace phosphor::power::format_utils
60