-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.hpp
731 lines (678 loc) · 17.5 KB
/
input.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#pragma once
//=====================================================================//
/*! @file
@brief input クラス @n
数値、文字列などの入力クラス @n
%b ---> 2進の数値 @n
%o ---> 8進の数値 @n
%d ---> 10進の数値 @n
%u ---> 符号無し10進(v111 で廃止、%d で自動対応) @n
%x ---> 16進の数値 @n
%f ---> 浮動小数点数(float、double) @n
%c ---> 1文字のキャラクター @n
%a ---> 自動、2進(bnnn)、8進(onnn)、10進、16進(xnnn)、浮動小数点を判別 @n
Exsample: @n
int v; @n
if(!(input("%d", parse_text) % v).status()) { @n
// Parse error. @n
} else { @n
// Parse OK! @n
format("%d\n") % v; @n
} @n
・変換時、オーバーフローした場合は、直前の値が出力される。 @n
・それ以外のエラーの場合、値は出力されない。 @n
・C++17 以降のコンパイラが必須 @n
・リトルエンディアン専用 @n
+ 2019/12/26 15:30- 数値のオート入力機能追加 @n
! 2020/01/05 02:52- 変換が失敗した場合に、引数の値を保持する @n
! 2020/01/05 03:22- %c の変換で、変換数カウントが変化しない不具合修正 @n
! 2020/01/05 06:16- %u 符号無し整数機能を追加(v111 で廃止) @n
+ 2020/01/15 10:03- std::string 型を定義 @n
+ 2020/01/24 15:15- 浮動小数点オーバーフローした場合のリミッター追加 @n
+ 2020/01/24 15:50- 整数変換でオーバーフローが発生したらエラーとする @n
+ 2020/01/25 17:33- 特殊制御文字を除外する「\」(バックスラッシュ)機能 @n
+ 2020/02/02 19:47- enum error など共有定義を継承 @n
! 2022/06/13 15:15- static const を static constexpr へ変更 @n
+ 2024/08/14 08:57- add header file (cstdint) @n
+ 2024/12/29 10:40+ cleanup @n
+ 2024/12/29 17:15+ Support 64 bits integer conversion (v110) @n
! 2024/12/30 23:50+ '%u' を廃止、エラーステートも一部廃止 (v111) @n
+ 2024/12/31 14:00+ '%a' における、浮動小数点を受け付け機能 (v112)
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <type_traits>
#include <unistd.h>
#include <cmath>
#include <string>
#include <cstdint>
#if (__cplusplus >= 201703L)
#include <string_view>
#endif
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 標準入力ファンクタ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class def_chainp {
const char* str_;
char last_;
bool unget_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] str 入力文字列(省力された場合「sci_getch」を使う)
*/
//-----------------------------------------------------------------//
def_chainp(const char* str = nullptr) : str_(str), last_(0), unget_(false) { }
//-----------------------------------------------------------------//
/*!
@brief 1文字戻す
*/
//-----------------------------------------------------------------//
void unget() {
unget_ = true;
}
//-----------------------------------------------------------------//
/*!
@brief ファンクタ(文字取得)
@return 文字
*/
//-----------------------------------------------------------------//
char operator() () {
if(unget_) {
unget_ = false;
} else {
if(str_ == nullptr) {
char ch;
if(read(STDIN_FILENO, &ch, 1) == 1) {
if(ch == '\n') ch = 0;
last_ = ch;
} else {
last_ = 0;
}
} else {
last_ = *str_;
if(last_ != 0) { ++str_; }
}
}
return last_;
}
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 汎用入力クラス・ベースクラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class input_base {
public:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief エラー種別
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class error : uint8_t {
none, ///< エラー無し
cha_sets, ///< 文字セットの不一致
partition, ///< 分離キャラクターの不一致
input_type, ///< 無効な入力タイプ
not_integer, ///< 整数型の不一致
sign_type, ///< 符号無し整数にマイナス符号
not_float, ///< 浮動小数点型の不一致
terminate, ///< 終端文字の不一致
overflow, ///< オーバーフロー
};
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 汎用入力クラス
@param[in] INP 文字入力クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class INP>
struct basic_input : public input_base {
static constexpr uint16_t VERSION = 112; ///< クラス・バージョン
private:
const char* form_;
INP inp_;
enum class mode : uint8_t {
NONE,
CHA,
BIN,
OCT,
DEC,
HEX,
REAL,
AUTO,
};
mode mode_;
error error_;
uint8_t nbc_;
bool ovf_;
int num_;
template <typename T>
T bin_() noexcept {
T a = 0;
constexpr auto lim = static_cast<T>(1) << (sizeof(T) * 8 - 1);
char ch;
while((ch = inp_()) != 0) {
if(ch >= '0' && ch <= '1') {
if((a & lim) != 0) {
ovf_ = true;
}
if(!ovf_) {
a <<= 1;
a += ch - '0';
}
} else if(nbc_ != 0 && ch == ' ') {
// 文字数指定がある場合「スペース」は '0' と同等に扱う
} else {
break;
}
if(nbc_ > 0) {
nbc_--;
if(nbc_ == 0) break;
}
}
return a;
}
template <typename T>
T oct_() noexcept {
T a = 0;
constexpr auto lim = static_cast<T>(0b111) << (sizeof(T) * 8 - 4);
char ch;
while((ch = inp_()) != 0) {
if(ch >= '0' && ch <= '7') {
if((a & lim) != 0) {
ovf_ = true;
}
if(!ovf_) {
a <<= 3;
a += ch - '0';
}
} else if(nbc_ != 0 && ch == ' ') {
// 文字数指定がある場合「スペース」は '0' と同等に扱う
} else {
break;
}
if(nbc_ > 0) {
nbc_--;
if(nbc_ == 0) break;
}
}
return a;
}
template <typename T>
T dec_() noexcept {
T a = 0;
char ch;
while((ch = inp_()) != 0) {
if(!ovf_ && ch >= '0' && ch <= '9') {
if constexpr (std::is_signed<T>::value) {
a *= 10;
a += ch - '0';
if(a < 0) {
ovf_ = true;
}
} else {
auto b = a;
a *= 10;
a += ch - '0';
if(b > a) {
ovf_ = true;
}
}
} else if(nbc_ != 0 && ch == ' ') {
// 文字数指定がある場合「スペース」は '0' と同等に扱う
} else {
break;
}
if(nbc_ > 0) {
nbc_--;
if(nbc_ == 0) break;
}
}
return a;
}
template <typename T>
T hex_() noexcept {
T a = 0;
constexpr auto lim = static_cast<T>(0b1111) << (sizeof(T) * 8 - 4);
char ch;
while((ch = inp_()) != 0) {
auto ovf = (a & lim) != 0;
if(ch >= 'a') { ch -= 0x20; }
if(ch >= '0' && ch <= '9') {
if(ovf) { ovf_ = true; }
if(!ovf_) {
a <<= 4;
a += ch - '0';
}
} else if(ch >= 'A' && ch <= 'F') {
if(ovf) { ovf_ = true; }
if(!ovf_) {
a <<= 4;
a += ch - 'A' + 10;
}
} else if(nbc_ != 0 && ch == ' ') {
// 文字数指定がある場合「スペース」は '0' と同等に扱う
} else {
break;
}
if(nbc_ > 0) {
nbc_--;
if(nbc_ == 0) break;
}
}
return a;
}
template <typename T>
T auto_num_() noexcept
{
enum class TYPE : uint8_t {
dec,
bin,
oct,
hex,
};
typedef typename std::make_unsigned<T>::type U;
char ch = inp_();
if(ch != '0') { // 最初に0がある場合無視
inp_.unget();
}
ch = inp_();
if(ch >= 0x60) { ch -= 0x20; }
T v = 0;
TYPE t(TYPE::dec);
if(ch == 'B') {
t = TYPE::bin;
} else if(ch == 'X') {
t = TYPE::hex;
} else if(ch == 'O') {
t = TYPE::oct;
} else {
inp_.unget();
}
switch(t) {
case TYPE::bin:
v = bin_<U>();
break;
case TYPE::oct:
v = oct_<U>();
break;
case TYPE::hex:
v = hex_<U>();
break;
default:
v = dec_<T>();
break;
}
return v;
}
template<typename T>
T real_() noexcept
{
struct real_t {
uint32_t a;
uint32_t b;
uint32_t c;
bool sign;
bool p;
bool ovf;
real_t() : a(0), b(0), c(1), sign(false), p(false), ovf(false) { }
void set_sign(bool sig = true) {
sign = sig;
}
void add(char ch)
{
if(c >= (0xffff'ffff / 10)) {
if(!p) ovf = true;
} else {
a *= 10;
a += static_cast<uint32_t>(ch - '0');
c *= 10;
}
}
void point() {
b = a;
a = 0;
c = 1;
p = true;
}
T get() const {
if(p) {
T tmp = static_cast<T>(b) + static_cast<T>(a) / static_cast<T>(c);
if(sign) return -tmp;
return tmp;
} else {
T tmp = static_cast<T>(a);
if(sign) return -tmp;
else return tmp;
}
}
};
real_t base;
real_t exp;
bool expf = false;
char ch;
while((ch = inp_()) != 0) {
if(ch == '+') {
if(expf) {
if(exp.a != 0) break;
exp.set_sign(false);
} else {
if(base.a != 0) break;
base.set_sign(false);
}
} else if(ch == '-') {
if(expf) {
if(exp.a != 0) break;
exp.set_sign();
} else {
if(base.a != 0) break;
base.set_sign();
}
} else if(ch >= '0' && ch <= '9') {
if(expf) {
exp.add(ch);
} else {
base.add(ch);
}
} else if(ch == '.') {
if(expf) {
if(exp.p) break;
exp.point();
} else {
if(base.p) break;
base.point();
}
} else if(ch == 'e' || ch == 'E') {
if(expf) break;
expf = true;
} else {
break;
}
}
if(base.ovf || exp.ovf) {
ovf_ = true;
}
if(expf) {
return base.get() * pow(static_cast<T>(10), exp.get());
} else {
return base.get();
}
}
void next_() noexcept
{
enum class fmm : uint8_t {
none,
type,
};
fmm cm = fmm::none;
char ch;
bool esc = false;
while((ch = *form_++) != 0) {
switch(cm) {
case fmm::none:
if(esc) {
esc = false;
if(ch != inp_()) {
error_ = error::partition;
return;
}
break;
}
if(ch == '\\') {
esc = true;
} else if(ch == '[') {
auto a = inp_();
const char* p = form_;
bool ok = false;
while((ch = *p++) != 0 && ch != ']') {
if(ch == a) ok = true;
}
if(ch == 0) --p;
form_ = p;
if(!ok) {
error_ = error::cha_sets;
return;
}
} else if(ch == '%' && *form_ != '%') {
cm = fmm::type;
nbc_ = 0;
} else if(ch != inp_()) {
error_ = error::partition;
return;
}
break;
case fmm::type:
if(ch >= 'a') ch -= 0x20;
if(ch >= '0' && ch <= '9') {
nbc_ *= 10;
nbc_ += ch - '0';
} else if(ch == 'B') {
mode_ = mode::BIN;
return;
} else if(ch == 'O') {
mode_ = mode::OCT;
return;
} else if(ch == 'D') {
mode_ = mode::DEC;
return;
} else if(ch == 'X') {
mode_ = mode::HEX;
return;
} else if(ch == 'F') {
mode_ = mode::REAL;
return;
} else if(ch == 'C') {
mode_ = mode::CHA;
return;
} else if(ch == 'A') {
mode_ = mode::AUTO;
return;
} else {
error_ = error::input_type;
return;
}
}
}
if(ch == 0 && inp_() == 0) {
} else {
error_ = error::terminate;
}
}
bool neg_() noexcept {
bool neg = false;
auto s = inp_();
if(s == '-') {
neg = true;
if(nbc_ > 0) nbc_--;
} else if(s == '+') {
neg = false;
if(nbc_ > 0) nbc_--;
} else {
inp_.unget();
}
return neg;
}
template <typename T>
T nb_dec_() noexcept
{
typedef typename std::make_unsigned<T>::type U;
typedef typename std::make_signed<T>::type S;
auto nbc = nbc_;
auto neg = neg_();
if(nbc > 0 && nbc_ == 0) {
error_ = error::not_integer;
return 0;
}
auto sign = std::is_signed<T>::value;
T v = 0;
switch(mode_) {
case mode::BIN:
v = bin_<U>();
break;
case mode::OCT:
v = oct_<U>();
break;
case mode::DEC:
if(!sign) {
if(neg) { // 符号無しにマイナス符号を付けた場合
error_ = error::sign_type;
return 0;
}
}
v = dec_<T>();
break;
case mode::HEX:
v = hex_<U>();
break;
case mode::AUTO:
v = auto_num_<T>();
break;
default:
error_ = error::not_integer;
break;
}
if(error_ == error::none) {
if(nbc == 0) {
inp_.unget();
}
next_();
}
if(neg) {
return -static_cast<S>(v);
} else {
return static_cast<S>(v);
}
}
template <typename T>
T nb_real_() noexcept
{
bool neg = neg_();
T v = 0.0f;
switch(mode_) {
case mode::REAL:
case mode::AUTO:
v = real_<T>();
break;
default:
error_ = error::not_float;
break;
}
if(error_ == error::none) {
inp_.unget();
next_();
}
if(neg) return -v;
else return v;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] form フォーマット式
@param[in] inp 変換文字列(nullptrの場合、sci_getch で取得)
*/
//-----------------------------------------------------------------//
basic_input(const char* form, const char* inp = nullptr) noexcept :
form_(form), inp_(inp),
mode_(mode::NONE), error_(error::none), nbc_(0), ovf_(false), num_(0)
{
next_();
}
//-----------------------------------------------------------------//
/*!
@brief コンストラクター std::string
@param[in] form フォーマット式
@param[in] inp 変換文字列(nullptrの場合、sci_getch で取得)
*/
//-----------------------------------------------------------------//
basic_input(const std::string& form, const::std::string& inp) noexcept :
basic_input(form.c_str(), (inp.empty() ? nullptr : inp.c_str())) { }
#if (__cplusplus >= 201703L)
//-----------------------------------------------------------------//
/*!
@brief コンストラクター std::string_view
@param[in] form フォーマット式
@param[in] inp 変換文字列(nullptrの場合、sci_getch で取得)
*/
//-----------------------------------------------------------------//
basic_input(const std::string_view& form, const::std::string_view& inp) noexcept :
basic_input(form.data(), (inp.empty() ? nullptr : inp.data())) { }
#endif
//-----------------------------------------------------------------//
/*!
@brief エラー種別を返す
@return エラー
*/
//-----------------------------------------------------------------//
error get_error() const noexcept { return error_; }
//-----------------------------------------------------------------//
/*!
@brief 変換ステータスを返す
@return 変換が全て正常なら「true」
*/
//-----------------------------------------------------------------//
bool status() const noexcept {
return error_ == error::none;
}
//-----------------------------------------------------------------//
/*!
@brief 正常変換数を取得
@return 正常変換数
*/
//-----------------------------------------------------------------//
int num() const noexcept { return num_; }
//-----------------------------------------------------------------//
/*!
@brief テンプレート・オペレーター「%」
@param[in] val 整数型
@return 自分の参照
*/
//-----------------------------------------------------------------//
template <typename T>
basic_input& operator % (T& val) noexcept
{
if(error_ != error::none) return *this;
if constexpr(std::is_floating_point<T>::value) {
auto tmp = nb_real_<T>();
if(error_ == error::none) {
++num_;
val = tmp;
}
} else {
if constexpr(sizeof(T) == 1) {
if(mode_ == mode::CHA) {
auto tmp = inp_();
if(error_ == error::none) {
++num_;
val = tmp;
}
next_();
} else {
error_ = error::input_type;
}
} else {
T tmp = nb_dec_<T>();
if(error_ == error::none) {
++num_;
val = tmp;
}
}
}
if(ovf_) {
error_ = error::overflow;
ovf_ = false;
}
return *this;
}
};
typedef basic_input<def_chainp> input;
}