Poplar and PopLibs
StringRef.hpp
1// Copyright (c) 2018 Graphcore Ltd. All rights reserved.
2#ifndef poplar_StringRef_hpp
3#define poplar_StringRef_hpp
4
5#include <poplar/ArrayRef.hpp>
6
7#include <cstring>
8#include <iosfwd>
9#include <string>
10
11namespace poplar {
12
13struct StringRef : public ArrayRef<char> {
14 constexpr StringRef() : ArrayRef() {}
15 constexpr StringRef(const StringRef &) = default;
16 StringRef(std::nullptr_t) = delete;
17 StringRef(const std::string &s) : ArrayRef(s.data(), s.size()) {}
18 constexpr StringRef(const char *p, std::size_t len) : ArrayRef(p, len) {}
19 StringRef(const char *p) : ArrayRef(p, std::strlen(p)) {}
20
21 // String literal will always be null terminated hence N-1:
22 template <std::size_t N>
23 constexpr StringRef(const char (&p)[N]) : ArrayRef(p, N - 1) {}
24
25 std::string cloneAsString() const { return std::string(begin(), size()); }
26
27 operator std::string() const { return cloneAsString(); }
28};
29
30inline bool operator==(StringRef l, StringRef r) {
31 if (l.size() != r.size()) {
32 return false;
33 }
34 return std::strncmp(l.data(), r.data(), l.size()) == 0;
35}
36
37inline bool operator!=(StringRef l, StringRef r) { return !(l == r); }
38
39inline bool operator<(StringRef l, StringRef r) {
40 // Include null terminator in comparison in the event one string is the prefix
41 // of the other.
42 return std::strncmp(l.data(), r.data(), std::min(l.size(), r.size()) + 1) < 0;
43}
44
45inline std::string operator+(StringRef l, StringRef r) {
46 std::string c;
47 c.reserve(l.size() + r.size());
48 c.append(l);
49 c.append(r.data(), r.size());
50 return c;
51}
52
53inline std::string &operator+=(std::string &s, StringRef r) {
54 s.append(r.begin(), r.end());
55 return s;
56}
57
58inline std::ostream &operator<<(std::ostream &os, const StringRef &s) {
59 return os << s.cloneAsString();
60}
61
62} // end namespace poplar
63
64#endif // poplar_StringRef_hpp
References to arrays.
half2 min(half2 src0, half2 src1)
Targets the f16v2min instruction.
Definition: ipu_intrinsics:384
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.