File tree Expand file tree Collapse file tree 5 files changed +110
-4
lines changed Expand file tree Collapse file tree 5 files changed +110
-4
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ add_subdirectory( src/tools/Log/test )
30
30
31
31
add_subdirectory ( src/tools/disco/BaseField/test )
32
32
add_subdirectory ( src/tools/disco/BaseFixedWidthField/test )
33
+ add_subdirectory ( src/tools/disco/Column/test )
33
34
add_subdirectory ( src/tools/disco/Real/test )
34
35
add_subdirectory ( src/tools/disco/Scientific/test )
35
36
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ add_cpp_test ( disco.Column Column.test.cpp )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 4
4
// system includes
5
5
#include < sstream>
6
6
#include < iomanip>
7
- #include < iostream>
8
7
9
8
// other includes
10
9
#include " fast_float/fast_float.h"
@@ -18,7 +17,7 @@ namespace disco {
18
17
* @brief A class for reading and writing fixed width data fields containing floating
19
18
* point values
20
19
*/
21
- template < unsigned int Width, unsigned int Precision, unsigned int Exponent = 2 >
20
+ template < unsigned int Width, unsigned int Precision >
22
21
class Scientific : public Real < Width > {
23
22
24
23
/* fields */
@@ -48,7 +47,7 @@ class Scientific : public Real< Width > {
48
47
}
49
48
else {
50
49
51
- unsigned int precision = Width - 4 - Exponent ;
50
+ unsigned int precision = Width - 6 ;
52
51
if ( value < 0 ) {
53
52
54
53
precision -= 1 ;
@@ -59,7 +58,7 @@ class Scientific : public Real< Width > {
59
58
// log10( significand ) is within [0,1[ so that exponent is given by the
60
59
// floor value
61
60
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 ;
63
62
}
64
63
precision = std::min ( precision, Precision );
65
64
You can’t perform that action at this time.
0 commit comments