Pakman
LineString.cc
1 #include <stdexcept>
2 
3 #include "LineString.h"
4 
5 // Construct from lvalue string
6 LineString::LineString(const std::string& raw_string) :
7  m_line_string(raw_string)
8 {
9  removeTrailingNewlines();
10  checkForNewlines();
11 }
12 
13 // Construct from rvalue string
14 LineString::LineString(std::string&& raw_string) :
15  m_line_string(std::move(raw_string))
16 {
17  removeTrailingNewlines();
18  checkForNewlines();
19 }
20 
21 // Construct from c-style string
22 LineString::LineString(const char raw_string[]) :
23  LineString(static_cast<std::string>(raw_string))
24 {
25 }
26 
27 // Remove trailing newlines
28 void LineString::removeTrailingNewlines()
29 {
30  // Class invariant: m_line_string has no trailing newlines
31  while (m_line_string.back() == '\n')
32  m_line_string.pop_back();
33 }
34 
35 // Check for newlines
36 void LineString::checkForNewlines() const
37 {
38  // Class invariant: m_line_string has no newlines
39  for (const char& c : m_line_string)
40  if (c == '\n')
41  throw std::runtime_error("LineString cannot contain newlines!");
42 }
43 
44 // Return string
45 const std::string& LineString::str() const
46 {
47  return m_line_string;
48 }
49 
50 // Return size
51 size_t LineString::size() const
52 {
53  return m_line_string.size();
54 }
LineString()=default
const std::string & str() const
Definition: LineString.cc:45
size_t size() const
Definition: LineString.cc:51