Dune Core Modules (2.9.0)

stringutility.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_STRINGUTILITY_HH
6#define DUNE_COMMON_STRINGUTILITY_HH
7
12#include <cstddef>
13#include <cstring>
14#include <algorithm>
15#include <cassert>
16#include <cstdio>
17#include <memory>
18#include <string>
19#include <new>
20
22
23
24namespace Dune {
25
36 template<typename C>
37 bool hasPrefix(const C& c, const char* prefix) {
38 std::size_t len = std::strlen(prefix);
39 return c.size() >= len &&
40 std::equal(prefix, prefix+len, c.begin());
41 }
42
52 template<typename C>
53 bool hasSuffix(const C& c, const char* suffix) {
54 std::size_t len = std::strlen(suffix);
55 if(c.size() < len) return false;
56 typename C::const_iterator it = c.begin();
57 std::advance(it, c.size() - len);
58 return std::equal(suffix, suffix+len, it);
59 }
60
72 template<class... T>
73 static std::string formatString(const std::string& s, const T&... args)
74 {
75 static const int bufferSize=1000;
76 char buffer[bufferSize];
77
78 // try to format with static buffer
79 int r = std::snprintf(buffer, bufferSize, s.c_str(), args...);
80
81 // negative return values correspond to errors
82 if (r<0)
83 DUNE_THROW(Dune::Exception,"Could not convert format string using given arguments.");
84
85 // if buffer was large enough return result as string
86 if (r<bufferSize)
87 return std::string(buffer);
88
89 // if buffer was to small allocate a larger buffer using
90 // the predicted size hint (+1 for the terminating 0-byte).
91 int dynamicBufferSize = r+1;
92
93 std::unique_ptr<char[]> dynamicBuffer;
94 try {
95 dynamicBuffer = std::make_unique<char[]>(dynamicBufferSize);
96 }
97 catch (const std::bad_alloc&) {
98 DUNE_THROW(Dune::Exception,"Could allocate large enough dynamic buffer in formatString.");
99 }
100
101 // convert and check for errors again
102 r = std::snprintf(dynamicBuffer.get(), dynamicBufferSize, s.c_str(), args...);
103 if (r<0)
104 DUNE_THROW(Dune::Exception,"Could not convert format string using given arguments.");
105
106 // the new buffer should always be large enough
107 assert(r<dynamicBufferSize);
108
109 return std::string(dynamicBuffer.get());
110 }
113}
114
115#endif // DUNE_COMMON_STRINGUTILITY_HH
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
bool hasSuffix(const C &c, const char *suffix)
Check whether a character container has a given suffix.
Definition: stringutility.hh:53
static std::string formatString(const std::string &s, const T &... args)
Format values according to printf format string.
Definition: stringutility.hh:73
bool hasPrefix(const C &c, const char *prefix)
Check whether a character container has a given prefix.
Definition: stringutility.hh:37
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)