Fcitx
inputbuffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_INPUTBUFFER_H_
8 #define _FCITX_UTILS_INPUTBUFFER_H_
9 
10 #include <cstdint>
11 #include <cstring>
12 #include <memory>
13 #include <string>
14 #include <string_view>
15 #include <utility>
16 #include <fcitx-utils/fcitxutils_export.h>
17 #include <fcitx-utils/flags.h>
18 #include <fcitx-utils/macros.h>
19 
20 /// \addtogroup FcitxUtils
21 /// \{
22 /// \file
23 /// \brief Generic InputBuffer to be used to handle user's preedit.
24 
25 namespace fcitx {
26 class InputBufferPrivate;
27 
28 enum class InputBufferOption {
29  /// No option.
30  NoOption = 0,
31  /// The input buffer is ascii character only, non ascii char will raise
32  /// exception.
33  AsciiOnly = 1,
34  /// Whether the input buffer only supports cursor at the end of buffer.
35  FixedCursor = 1 << 1
36 };
37 
39 
40 /// A string buffer that come with convinient functions to handle use input.
41 class FCITXUTILS_EXPORT InputBuffer {
42 public:
43  /// Create a input buffer with options.
44  /// \see InputBufferOption
46  virtual ~InputBuffer();
47 
48  /// Get the buffer option.
49  InputBufferOptions options() const;
50 
51  /// Type a C-String with length into buffer.
52  bool type(const char *s, size_t length) { return typeImpl(s, length); }
53  /// Type an std::stirng to buffer.
54  bool type(const std::string &s) { return type(s.c_str(), s.size()); }
55  /// Type a C-String to buffer.
56  bool type(const char *s) { return type(s, std::strlen(s)); }
57  /// Type a ucs4 character to buffer.
58  bool type(uint32_t unicode);
59 
60  /// Erase a range of character.
61  virtual void erase(size_t from, size_t to);
62  /// Set cursor position, by character.
63  virtual void setCursor(size_t cursor);
64 
65  /// Get the max size of the buffer.
66  size_t maxSize() const;
67 
68  /// Set max size of the buffer.
69  void setMaxSize(size_t s);
70 
71  /// Utf8 string in the buffer.
72  const std::string &userInput() const;
73 
74  /// Cursor position by utf8 character.
75  size_t cursor() const;
76 
77  /// Cursor position by char (byte).
78  size_t cursorByChar() const;
79 
80  /// Size of buffer, by number of utf8 character.
81  size_t size() const;
82 
83  /// UCS-4 char in the buffer. Will raise exception if i is out of range.
84  uint32_t charAt(size_t i) const;
85 
86  /// Byte range for character at position i.
87  std::pair<size_t, size_t> rangeAt(size_t i) const;
88 
89  // String view for char
90  std::string_view viewAt(size_t i) const;
91 
92  /// Byte size at position i.
93  size_t sizeAt(size_t i) const;
94 
95  /// Whether buffer is empty.
96  bool empty() const { return size() == 0; }
97 
98  /// Helper function to implement "delete" key.
99  inline bool del() {
100  auto c = cursor();
101  if (c < size()) {
102  erase(c, c + 1);
103  return true;
104  }
105  return false;
106  }
107 
108  /// Helper function to implement "backspace" key.
109  inline bool backspace() {
110  auto c = cursor();
111  if (c > 0) {
112  erase(c - 1, c);
113  return true;
114  }
115  return false;
116  }
117 
118  /// Clear all buffer.
119  void clear() { erase(0, size()); }
120 
121  /// Save memory by call shrink to fit to internal buffer.
122  void shrinkToFit();
123 
124 protected:
125  /// Type a certain length of utf8 character to the buffer. [s, s+length]
126  /// need to be valid utf8 string.
127  virtual bool typeImpl(const char *s, size_t length);
128 
129 private:
130  std::unique_ptr<InputBufferPrivate> d_ptr;
131  FCITX_DECLARE_PRIVATE(InputBuffer);
132 };
133 } // namespace fcitx
134 
135 #endif // _FCITX_UTILS_INPUTBUFFER_H_
A string buffer that come with convinient functions to handle use input.
Definition: inputbuffer.h:41
bool del()
Helper function to implement "delete" key.
Definition: inputbuffer.h:99
bool type(const char *s, size_t length)
Type a C-String with length into buffer.
Definition: inputbuffer.h:52
Whether the input buffer only supports cursor at the end of buffer.
size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.
Definition: utf8.h:33
void clear()
Clear all buffer.
Definition: inputbuffer.h:119
Definition: action.cpp:17
InputBufferOption
Definition: inputbuffer.h:28
bool empty() const
Whether buffer is empty.
Definition: inputbuffer.h:96
bool type(const std::string &s)
Type an std::stirng to buffer.
Definition: inputbuffer.h:54
Class provides bit flag support for Enum.
Definition: flags.h:33
bool backspace()
Helper function to implement "backspace" key.
Definition: inputbuffer.h:109
Helper template class to make easier to use type safe enum flags.
bool type(const char *s)
Type a C-String to buffer.
Definition: inputbuffer.h:56
The input buffer is ascii character only, non ascii char will raise exception.