blob: e1624d7a0daba7fa796208c625d5b8e742f0455e (
plain)
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
|
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Keypad.h"
#include <AK/StringBuilder.h>
#include <LibCrypto/BigFraction/BigFraction.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibCrypto/NumberTheory/ModularFunctions.h>
void Keypad::type_digit(int digit)
{
switch (m_state) {
case State::External:
m_state = State::TypingInteger;
m_int_value = digit;
m_frac_value.set_to_0();
m_frac_length.set_to_0();
break;
case State::TypingInteger:
VERIFY(m_frac_value == 0);
VERIFY(m_frac_length == 0);
m_int_value.set_to(m_int_value.multiplied_by(10));
m_int_value.set_to(m_int_value.plus(digit));
break;
case State::TypingDecimal:
m_frac_value.set_to(m_frac_value.multiplied_by(10));
m_frac_value.set_to(m_frac_value.plus(digit));
m_frac_length.set_to(m_frac_length.plus(1));
break;
}
}
void Keypad::type_decimal_point()
{
switch (m_state) {
case State::External:
m_int_value.set_to_0();
m_frac_value.set_to_0();
m_frac_length.set_to_0();
m_state = State::TypingDecimal;
break;
case State::TypingInteger:
VERIFY(m_frac_value == 0);
VERIFY(m_frac_length == 0);
m_state = State::TypingDecimal;
break;
case State::TypingDecimal:
// Ignore it.
break;
}
}
void Keypad::type_backspace()
{
switch (m_state) {
case State::External:
m_int_value.set_to_0();
m_frac_value.set_to_0();
m_frac_length.set_to_0();
break;
case State::TypingDecimal:
if (m_frac_length > 0) {
m_frac_value.set_to(m_frac_value.divided_by(10).quotient);
m_frac_length.set_to(m_frac_length.minus(1));
break;
}
VERIFY(m_frac_value == 0);
m_state = State::TypingInteger;
[[fallthrough]];
case State::TypingInteger:
VERIFY(m_frac_value == 0);
VERIFY(m_frac_length == 0);
m_int_value.set_to(m_int_value.divided_by(10).quotient);
break;
}
}
Crypto::BigFraction Keypad::value() const
{
if (m_state != State::External) {
Crypto::SignedBigInteger sum { m_int_value.multiplied_by(Crypto::NumberTheory::Power("10"_bigint, m_frac_length)).plus(m_frac_value) };
Crypto::BigFraction res { move(sum), Crypto::NumberTheory::Power("10"_bigint, m_frac_length) };
m_internal_value = move(res);
}
return m_internal_value;
}
void Keypad::set_value(Crypto::BigFraction value)
{
m_state = State::External;
m_internal_value = move(value);
}
void Keypad::set_to_0()
{
m_int_value.set_to_0();
m_frac_value.set_to_0();
m_frac_length.set_to_0();
m_internal_value.set_to_0();
m_state = State::External;
}
String Keypad::to_string() const
{
if (m_state == State::External)
return m_internal_value.to_string(m_displayed_fraction_length);
StringBuilder builder;
String const integer_value = m_int_value.to_base(10);
String const frac_value = m_frac_value.to_base(10);
unsigned const number_pre_zeros = m_frac_length.to_u64() - (frac_value.length() - 1) - (frac_value == "0" ? 0 : 1);
builder.append(integer_value);
// NOTE: We test for the state so the decimal point appears on screen as soon as you type it.
if (m_state == State::TypingDecimal) {
builder.append('.');
builder.append_repeated('0', number_pre_zeros);
if (frac_value != "0")
builder.append(frac_value);
}
return builder.to_string();
}
void Keypad::set_rounding_length(unsigned rounding_threshold)
{
m_displayed_fraction_length = rounding_threshold;
}
|