Dune Core Modules (2.4.1)

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#ifndef DUNE_COMMON_STRINGUTILITY_HH
4#define DUNE_COMMON_STRINGUTILITY_HH
5
10#include <cstddef>
11#include <cstring>
12#include <algorithm>
13#include <cassert>
14#include <cstdio>
15#include <memory>
16#include <string>
17#include <new>
18
20#include <dune/common/std/memory.hh>
21
22
23namespace Dune {
24
35 template<typename C>
36 bool hasPrefix(const C& c, const char* prefix) {
37 std::size_t len = std::strlen(prefix);
38 return c.size() >= len &&
39 std::equal(prefix, prefix+len, c.begin());
40 }
41
51 template<typename C>
52 bool hasSuffix(const C& c, const char* suffix) {
53 std::size_t len = std::strlen(suffix);
54 if(c.size() < len) return false;
55 typename C::const_iterator it = c.begin();
56 std::advance(it, c.size() - len);
57 return std::equal(suffix, suffix+len, it);
58 }
59
71 template<class... T>
72 static std::string formatString(const std::string& s, const T&... args)
73 {
74 static const int bufferSize=1000;
75 char buffer[bufferSize];
76
77 // try to format with static buffer
78 int r = std::snprintf(buffer, bufferSize, s.c_str(), args...);
79
80 // negative return values correspond to errors
81 if (r<0)
82 DUNE_THROW(Dune::Exception,"Could not convert format string using given arguments.");
83
84 // if buffer was large enough return result as string
85 if (r<bufferSize)
86 return std::string(buffer);
87
88 // if buffer was to small allocate a larger buffer using
89 // the predicted size hint (+1 for the terminating 0-byte).
90 int dynamicBufferSize = r+1;
91
92 std::unique_ptr<char[]> dynamicBuffer;
93 try {
94 dynamicBuffer = Dune::Std::make_unique<char[]>(dynamicBufferSize);
95 }
96 catch (const std::bad_alloc&) {
97 DUNE_THROW(Dune::Exception,"Could allocate large enough dynamic buffer in formatString.");
98 }
99
100 // convert and check for errors again
101 r = std::snprintf(dynamicBuffer.get(), dynamicBufferSize, s.c_str(), args...);
102 if (r<0)
103 DUNE_THROW(Dune::Exception,"Could not convert format string using given arguments.");
104
105 // the new buffer should always be large enough
106 assert(r<dynamicBufferSize);
107
108 return std::string(dynamicBuffer.get());
109 }
112}
113
114#endif // DUNE_COMMON_STRINGUTILITY_HH
Base class for Dune-Exceptions.
Definition: exceptions.hh:91
A few common exception classes.
bool hasSuffix(const C &c, const char *suffix)
Check whether a character container has a given suffix.
Definition: stringutility.hh:52
static std::string formatString(const std::string &s, const T &... args)
Format values according to printf format string.
Definition: stringutility.hh:72
bool hasPrefix(const C &c, const char *prefix)
Check whether a character container has a given prefix.
Definition: stringutility.hh:36
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
Dune namespace.
Definition: alignment.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)