Skip to content

Commit b99481b

Browse files
committed
Adding Column to skip columns
1 parent c530cd2 commit b99481b

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

cmake/unit_testing.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_subdirectory( src/tools/Log/test )
3030

3131
add_subdirectory( src/tools/disco/BaseField/test )
3232
add_subdirectory( src/tools/disco/BaseFixedWidthField/test )
33+
add_subdirectory( src/tools/disco/Column/test )
3334
add_subdirectory( src/tools/disco/Real/test )
3435
add_subdirectory( src/tools/disco/Scientific/test )
3536

src/tools/disco/Column.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef NJOY_TOOLS_DISCO_FIXEDWIDTHSCIENTIFIC
2+
#define NJOY_TOOLS_DISCO_FIXEDWIDTHSCIENTIFIC
3+
4+
// system includes
5+
#include <sstream>
6+
#include <iomanip>
7+
8+
// other includes
9+
#include "tools/disco/BaseFixedWidthField.hpp"
10+
11+
namespace njoy {
12+
namespace tools {
13+
namespace disco {
14+
15+
/**
16+
* @brief A class for reading and writing fixed width data fields containing floating
17+
* point values
18+
*/
19+
template< unsigned int Width >
20+
class Column : public BaseFixedWidthField< Width > {
21+
22+
/* fields */
23+
24+
protected:
25+
26+
using BaseFixedWidthField< Width >::isSpace;
27+
using BaseFixedWidthField< Width >::isSpaceOrTabulation;
28+
using BaseFixedWidthField< Width >::isWhiteSpace;
29+
using BaseFixedWidthField< Width >::isNewLine;
30+
using BaseFixedWidthField< Width >::isEndOfFile;
31+
using BaseFixedWidthField< Width >::skipSpaces;
32+
using BaseFixedWidthField< Width >::skipPlusSign;
33+
34+
public:
35+
36+
template < typename Iterator >
37+
static void read( Iterator& iter, const Iterator& ) {
38+
39+
unsigned int position = 0;
40+
while( position < Width && ! ( isNewLine( iter ) || isEndOfFile( iter ) ) ) {
41+
42+
++position;
43+
++iter;
44+
}
45+
}
46+
47+
template< typename Iterator >
48+
static void write( Iterator& iter ) {
49+
50+
unsigned int position = 0;
51+
while ( position < Width ) {
52+
53+
++position;
54+
*iter++ = ' ';
55+
}
56+
}
57+
};
58+
59+
} // disco namespace
60+
} // tools namespace
61+
} // njoy namespace
62+
63+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_cpp_test( disco.Column Column.test.cpp )
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// include Catch2
2+
#include <catch2/catch_test_macros.hpp>
3+
4+
// what we are testing
5+
#include "tools/disco/Column.hpp"
6+
7+
// other includes
8+
9+
// convenience typedefs
10+
using namespace njoy::tools::disco;
11+
12+
SCENARIO( "Column" ) {
13+
14+
std::string string;
15+
auto begin = string.begin();
16+
auto end = string.end();
17+
18+
string = " ";
19+
begin = string.begin();
20+
end = string.end();
21+
Column< 1 >::read( begin, end );
22+
CHECK( begin == end - 3 );
23+
Column< 3 >::read( begin, end );
24+
CHECK( begin == end );
25+
26+
string = "abcd";
27+
begin = string.begin();
28+
end = string.end();
29+
Column< 1 >::read( begin, end );
30+
CHECK( begin == end - 3 );
31+
Column< 3 >::read( begin, end );
32+
CHECK( begin == end );
33+
34+
string = " \n";
35+
begin = string.begin();
36+
end = string.end();
37+
Column< 1 >::read( begin, end );
38+
CHECK( begin == end - 2 );
39+
Column< 3 >::read( begin, end );
40+
CHECK( begin == end - 1 );
41+
42+
} // SCENARIO

src/tools/disco/Scientific.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// system includes
55
#include <sstream>
66
#include <iomanip>
7-
#include <iostream>
87

98
// other includes
109
#include "fast_float/fast_float.h"
@@ -18,7 +17,7 @@ namespace disco {
1817
* @brief A class for reading and writing fixed width data fields containing floating
1918
* point values
2019
*/
21-
template < unsigned int Width, unsigned int Precision, unsigned int Exponent = 2 >
20+
template < unsigned int Width, unsigned int Precision >
2221
class Scientific : public Real< Width > {
2322

2423
/* fields */
@@ -48,7 +47,7 @@ class Scientific : public Real< Width > {
4847
}
4948
else {
5049

51-
unsigned int precision = Width - 4 - Exponent;
50+
unsigned int precision = Width - 6;
5251
if ( value < 0 ) {
5352

5453
precision -= 1;
@@ -59,7 +58,7 @@ class Scientific : public Real< Width > {
5958
// log10( significand ) is within [0,1[ so that exponent is given by the
6059
// floor value
6160
const int exponent = static_cast< int >( std::floor( std::log10( absValue ) ) );
62-
precision -= static_cast< int >( std::floor( std::log10( std::abs( exponent ) ) ) ) - Exponent + 1;
61+
precision -= static_cast< int >( std::floor( std::log10( std::abs( exponent ) ) ) ) - 1;
6362
}
6463
precision = std::min( precision, Precision );
6564

0 commit comments

Comments
 (0)