File indexing completed on 2024-12-29 04:11:43

0001 /***************************************************************************
0002  *                                                                         *
0003  *   Copyright : (C) 2010 The University of Toronto                        *
0004  *   email     : netterfield@astro.utoronto.ca                             *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #ifndef ASCII_CHARACTER_TRAITS_H
0014 #define ASCII_CHARACTER_TRAITS_H
0015 
0016 #include <QString>
0017 
0018 namespace AsciiCharacterTraits
0019 {
0020 
0021 struct LineEndingType {
0022   inline LineEndingType() {}
0023   bool is_crlf;
0024   char character;
0025   inline bool isCR() const { return character == '\r'; }
0026   inline bool isLF() const { return character == '\n'; }
0027 };
0028 
0029 // column and comment delimiter functions
0030 
0031 struct AlwaysTrue {
0032   inline AlwaysTrue() {}
0033   inline bool operator()() const {
0034     return true;
0035   }
0036 };
0037 
0038 struct AlwaysFalse {
0039   inline AlwaysFalse() {}
0040   inline bool operator()() const {
0041     return false;
0042   }
0043 };
0044 
0045 struct NoDelimiter {
0046   inline NoDelimiter() {}
0047   inline bool operator()(const char) const {
0048     return false;
0049   }
0050 };
0051 
0052 struct  IsWhiteSpace {
0053   inline IsWhiteSpace() {}
0054   inline bool operator()(const char c) const {
0055     return c == ' ' || c == '\t';
0056   }
0057 };
0058 
0059 struct IsDigit {
0060   inline IsDigit() {}
0061   inline bool operator()(const char c) const {
0062     return (c >= 48) && (c <= 57) ? true : false;
0063   }
0064 };
0065 
0066 struct IsCharacter {
0067   inline IsCharacter(char c) : character(c) {}
0068   const char character;
0069   inline bool operator()(const char c) const {
0070     return character == c;
0071   }
0072 };
0073 
0074 struct IsInString {
0075   inline IsInString(const QString& s) : str(s), chars(s.size()) {
0076     QByteArray ascii = str.toLatin1();
0077     for (int i = 0; i < 6 && i < chars; i++) {
0078       ch[i] = ascii[i];
0079     }
0080   }
0081   const QString str;
0082   const int chars;
0083   char ch[6];
0084   inline bool operator()(const char c) const {
0085     switch (chars) {
0086     case 0: return false;
0087     case 1: return ch[0]==c;
0088     case 2: return ch[0]==c || ch[1]==c;
0089     case 3: return ch[0]==c || ch[1]==c || ch[2]==c;
0090     case 4: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c;
0091     case 5: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c || ch[4]==c;
0092     case 6: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c || ch[4]==c || ch[5]==c;
0093     default: return str.contains(c);
0094     }
0095   }
0096 };
0097 
0098 struct IsLineBreakLF {
0099   inline IsLineBreakLF(const LineEndingType&) : size(1) {}
0100   const int size;
0101   inline bool operator()(const char c) const {
0102     return c == '\n';
0103   }
0104 };
0105 
0106 struct IsLineBreakCR {
0107   inline IsLineBreakCR(const LineEndingType& t) : size( t.is_crlf ? 2 : 1 ) {}
0108   const int size;
0109   inline bool operator()(const char c) const {
0110     return c == '\r';
0111   }
0112 };
0113 
0114 }
0115 
0116 #endif
0117 // vim: ts=2 sw=2 et