Skip to content

Commit

Permalink
Capitalisation of custom types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Apr 8, 2023
1 parent 719b08c commit 04e7dde
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 79 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ API documentation
Type definitions
----------------

.. doxygentypedef:: ccp
.. doxygentypedef:: ccpc
.. doxygentypedef:: CCP
.. doxygentypedef:: CCPC


Functions
Expand Down
18 changes: 9 additions & 9 deletions src/textparser.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#include "textparser.h"


TextParser::TextParser(ccpc delimiter)
TextParser::TextParser(CCPC delimiter)
: delimiter_ {delimiter} {}

TextParser::TextParser(ccpc delimiter, ccpc eol)
TextParser::TextParser(CCPC delimiter, CCPC eol)
: delimiter_ {delimiter}, eol_ {eol} {}


void TextParser::parse(char& result, ccpc begin, ccpc end) const {
void TextParser::parse(char& result, CCPC begin, CCPC end) const {
result = 0;
if (begin < end) {
result = *begin;
}
}

void TextParser::parse(double& result, ccpc begin, ccpc) const {
void TextParser::parse(double& result, CCPC begin, CCPC) const {
result = strtod(begin, nullptr);
}

void TextParser::parse(float& result, ccpc begin, ccpc) const {
void TextParser::parse(float& result, CCPC begin, CCPC) const {
result = strtod(begin, nullptr);
}


void TextParser::consume_(ccp* line) const {
for (ccp p {delimiter_}; *p and **line and *p == **line; p++, (*line)++);
void TextParser::consume_(CCP* line) const {
for (CCP p {delimiter_}; *p and **line and *p == **line; p++, (*line)++);
}

ccp TextParser::findEnd_(ccp line) const {
ccp end {strstr(line, delimiter_)};
CCP TextParser::findEnd_(CCP line) const {
CCP end {strstr(line, delimiter_)};
if (not (end or (eol_ and (end = strstr(line, eol_))))) {
return line + strlen(line);
}
Expand Down
92 changes: 46 additions & 46 deletions src/textparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class TextParser {
*
* \param[in] delimiter Field delimiter.
*/
TextParser(ccpc delimiter);
TextParser(CCPC delimiter);

/*! \copydoc TextParser(ccpc)
/*! \copydoc TextParser(CCPC)
*
* \param[in] eol Line delimiter.
*/
TextParser(ccpc delimiter, ccpc eol);
TextParser(CCPC delimiter, CCPC eol);


/*! Parse a field.
Expand All @@ -26,55 +26,55 @@ class TextParser {
* \param[in] begin Pointer to C string.
* \param[in] end Pointer to end of C string.
*/
void parse(char& result, ccpc begin, ccpc end) const;
void parse(char& result, CCPC begin, CCPC end) const;

/*! \copydoc parse(char&, ccpc, ccpc) const */
void parse(double& result, ccpc begin, ccpc end) const;
/*! \copydoc parse(char&, CCPC, CCPC) const */
void parse(double& result, CCPC begin, CCPC end) const;

/*! \copydoc parse(char&, ccpc, ccpc) const */
void parse(float& result, ccpc begin, ccpc end) const;
/*! \copydoc parse(char&, CCPC, CCPC) const */
void parse(float& result, CCPC begin, CCPC end) const;

/*!
* \tparam n String length.
*
* \copydoc parse(char&, ccpc, ccpc) const
* \copydoc parse(char&, CCPC, CCPC) const
*/
template <size_t n>
void parse(char (&result)[n], ccpc begin, ccpc end) const;
void parse(char (&result)[n], CCPC begin, CCPC end) const;

/*!
* \tparam truth Truth value.
*
* \copydoc parse(char&, ccpc, ccpc) const
* \copydoc parse(char&, CCPC, CCPC) const
*/
template <ccp truth>
void parse(Bool<truth>& result, ccpc begin, ccpc end) const;
template <CCP truth>
void parse(Bool<truth>& result, CCPC begin, CCPC end) const;

/*!
* \tparam T Integer type.
* \tparam base Integer base.
*
* \copydoc parse(char&, ccpc, ccpc) const
* \copydoc parse(char&, CCPC, CCPC) const
*/
template <class T, size_t base>
void parse(Number<T, base>& result, ccpc begin, ccpc end) const;
void parse(Number<T, base>& result, CCPC begin, CCPC end) const;

/*!
* \tparam T Integer type.
* \tparam labels Labels.
*
* \copydoc parse(char&, ccpc, ccpc) const
* \copydoc parse(char&, CCPC, CCPC) const
*/
template <class T, ccp* labels>
void parse(Category<T, labels>& result, ccpc begin, ccpc end) const;
template <class T, CCP* labels>
void parse(Category<T, labels>& result, CCPC begin, CCPC end) const;

/*!
* \tparam T Integer type.
*
* \copydoc parse(char&, ccpc, ccpc) const
* \copydoc parse(char&, CCPC, CCPC) const
*/
template <class T>
void parse(T& result, ccpc begin, ccpc end) const;
void parse(T& result, CCPC begin, CCPC end) const;


/*! Parse a line.
Expand All @@ -85,45 +85,45 @@ class TextParser {
* \param[out] args Variables to hold the parsed data.
*/
template <class... Ts>
void parseLine(ccpc line, Ts&... args) const;
void parseLine(CCPC line, Ts&... args) const;

private:
void consume_(ccp*) const;
ccp findEnd_(ccp) const;
void consume_(CCP*) const;
CCP findEnd_(CCP) const;

template <class T>
void parseField_(ccp*, T&) const;
void parseField_(CCP*, T&) const;

inline void parseLine_(ccp*) const;
inline void parseLine_(CCP*) const;
template <size_t n, class... Ts>
void parseLine_(ccp*, char (&)[n], Ts&...) const;
void parseLine_(CCP*, char (&)[n], Ts&...) const;
template <class T, size_t n, class... Ts>
void parseLine_(ccp*, T (&)[n], Ts&...) const;
void parseLine_(CCP*, T (&)[n], Ts&...) const;
template <class T, class... Ts>
void parseLine_(ccp*, T&, Ts&...) const;
void parseLine_(CCP*, T&, Ts&...) const;

ccp delimiter_ {};
ccp eol_ {nullptr};
CCP delimiter_ {};
CCP eol_ {nullptr};
};


template <size_t n>
void TextParser::parse(char (&result)[n], ccpc begin, ccpc end) const {
void TextParser::parse(char (&result)[n], CCPC begin, CCPC end) const {
char* p {result};
for (ccp q {begin}; p < result + n - 1 and q < end; p++, q++) {
for (CCP q {begin}; p < result + n - 1 and q < end; p++, q++) {
*p = *q;
}
*p = 0;
}

template <ccp truth>
void TextParser::parse(Bool<truth>& result, ccpc begin, ccpc end) const {
template <CCP truth>
void TextParser::parse(Bool<truth>& result, CCPC begin, CCPC end) const {
result.value = strmatch(begin, end, truth);
}

template <class T, ccp* labels>
template <class T, CCP* labels>
void TextParser::parse(
Category<T, labels>& result, ccpc begin, ccpc end) const {
Category<T, labels>& result, CCPC begin, CCPC end) const {
result.value = -1;
for (size_t i {0}; labels[i]; i++) {
if (strmatch(begin, end, labels[i])) {
Expand All @@ -134,50 +134,50 @@ void TextParser::parse(
}

template <class T, size_t base>
void TextParser::parse(Number<T, base>& result, ccpc begin, ccpc) const {
void TextParser::parse(Number<T, base>& result, CCPC begin, CCPC) const {
result.value = strtol(begin, nullptr, base);
}

template <class T>
void TextParser::parse(T& result, ccpc begin, ccpc) const {
void TextParser::parse(T& result, CCPC begin, CCPC) const {
result = strtol(begin, nullptr, 10);
}


template <class... Ts>
void TextParser::parseLine(ccpc line, Ts&... args) const {
ccp line_ {line};
void TextParser::parseLine(CCPC line, Ts&... args) const {
CCP line_ {line};
parseLine_(&line_, args...);
}


template <class T>
void TextParser::parseField_(ccp* line, T& data) const {
ccpc end {findEnd_(*line)};
void TextParser::parseField_(CCP* line, T& data) const {
CCPC end {findEnd_(*line)};
parse(data, *line, end);
*line = end;
consume_(line);
}


inline void TextParser::parseLine_(ccp*) const {}
inline void TextParser::parseLine_(CCP*) const {}

template <size_t n, class... Ts>
void TextParser::parseLine_(ccp* line, char (&arr)[n], Ts&... tail) const {
void TextParser::parseLine_(CCP* line, char (&arr)[n], Ts&... tail) const {
parseField_(line, arr);
parseLine_(line, tail...);
}

template <class T, size_t n, class... Ts>
void TextParser::parseLine_(ccp* line, T (&arr)[n], Ts&... tail) const {
void TextParser::parseLine_(CCP* line, T (&arr)[n], Ts&... tail) const {
for (T& element: arr) {
parseField_(line, element);
}
parseLine_(line, tail...);
}

template <class T, class... Ts>
void TextParser::parseLine_(ccp* line, T& head, Ts&... tail) const {
void TextParser::parseLine_(CCP* line, T& head, Ts&... tail) const {
parseField_(line, head);
parseLine_(line, tail...);
}
6 changes: 3 additions & 3 deletions src/texttypes.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "texttypes.h"

bool strmatch(ccpc begin, ccpc end, ccpc str) {
ccp p {begin};
ccp q {str};
bool strmatch(CCPC begin, CCPC end, CCPC str) {
CCP p {begin};
CCP q {str};
for (; p < end and *q and *p == *q; p++, q++);
return p == end and not *q;
}
10 changes: 5 additions & 5 deletions src/texttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <stdlib.h>

using ccp = char const*; //!< Pointer to a constant string.
using ccpc = ccp const; //!< Constant pointer to a constant string.
using CCP = char const*; //!< Pointer to a constant string.
using CCPC = CCP const; //!< Constant pointer to a constant string.

/*! Generic container.
*
Expand All @@ -26,15 +26,15 @@ struct Number : Container_<T> {};
*
* \tparam truth Truth value.
*/
template <ccp truth>
template <CCP truth>
struct Bool : Container_<bool> {};

/*! Category.
*
* \tparam T Integer type.
* \tparam labels Labels.
*/
template <class T, ccp* labels>
template <class T, CCP* labels>
struct Category : Container_<T> {};


Expand All @@ -44,4 +44,4 @@ struct Category : Container_<T> {};
* \param[in] end Pointer to end of the first C string.
* \param[in] str Pointer to the second C string.
*/
bool strmatch(ccpc begin, ccpc end, ccpc str);
bool strmatch(CCPC begin, CCPC end, CCPC str);
Loading

0 comments on commit 04e7dde

Please sign in to comment.