Tinker9 70bd052 (Thu Nov 9 12:11:35 2023 -0800)
Loading...
Searching...
No Matches
iofortstr.h
1#pragma once
2#include <cstring>
3#include <string>
4
5namespace tinker {
10{
11private:
12 char* const m_b; // begin in [begin, end)
13 char* const m_e; // end in [begin, end)
14
15 FortranStringView() = delete; // no default constructor
16
17 // If `dst != src`, copy the `first_n` characters from `src` to `dst`;
18 // fill `dst` with blanks if `first_n` is less than `dstlen`;
19 // `dst` is NOT NULL-terminated.
20 static void copyWithBlank(char* dst, size_t dstlen, const char* src,
21 size_t first_n);
22
23 // Compare to a string `src` of `len`.
24 // The shorter string is filled by blanks prior to comparison.
25 bool ifEq(const char* src, size_t len) const;
26
27 // Returns the max number of characters in the string
28 // including the trailing whitespaces.
29 size_t size() const;
30
31public:
35 template <size_t Len>
36 FortranStringView(char (&src)[Len])
37 : m_b(src)
38 , m_e(m_b + Len)
39 {}
40 FortranStringView(char* src, size_t len);
42
46 template <size_t Len>
47 FortranStringView& operator=(const char (&src)[Len])
48 {
49 copyWithBlank(m_b, size(), src, Len);
50 return *this;
51 }
52 FortranStringView& operator=(const char* src);
53 FortranStringView& operator=(const std::string& src);
55
58 template <size_t Len>
59 bool operator==(const char (&src)[Len]) const
60 {
61 return ifEq(src, Len);
62 }
63 bool operator==(const char* src) const;
64 bool operator==(const std::string& src) const;
65 bool operator==(const FortranStringView& src) const;
67
69 size_t len_trim() const;
70
72 std::string trim() const;
73
78 FortranStringView operator()(int begin1, int back1) const;
79
83 FortranStringView operator()(int begin1) const;
84};
85
89}
bool operator==(const char *src) const
Returns true if two strings are equal.
FortranStringView(char *src, size_t len)
Constructs with a char pointer and the reserved length of the Fortran string.
bool operator==(const FortranStringView &src) const
Returns true if two strings are equal.
std::string trim() const
Returns a string without the trailing whitespaces.
FortranStringView operator()(int begin1, int back1) const
Analogous to Fortran str(x:y) syntax.
bool operator==(const char(&src)[Len]) const
Returns true if two strings are equal.
Definition: iofortstr.h:59
FortranStringView & operator=(const char(&src)[Len])
Assigned by a C/C++ style string. Ignores the trailing characters if the source is too long.
Definition: iofortstr.h:47
FortranStringView & operator=(const char *src)
Assigned by a C/C++ style string. Ignores the trailing characters if the source is too long.
bool operator==(const std::string &src) const
Returns true if two strings are equal.
size_t len_trim() const
Returns the length of string, ignoring the trailing whitespaces.
FortranStringView(char(&src)[Len])
Constructs with a char pointer and the reserved length of the Fortran string.
Definition: iofortstr.h:36
FortranStringView operator()(int begin1) const
Analogous to Fortran str(x:) syntax.
FortranStringView & operator=(const std::string &src)
Assigned by a C/C++ style string. Ignores the trailing characters if the source is too long.
Reference to the non-NULL-terminated Fortran strings. Provides a few std::string-style methods to han...
Definition: iofortstr.h:10
Definition: testrt.h:9