DUNE PDELab (2.8)

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