summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2011-12-02 20:54:50 +0100
committerLars-Dominik Braun <lars@6xq.net>2011-12-03 16:40:25 +0100
commitcc2e11de05ffce5043ee19cfbe33fdddef3e5b5a (patch)
tree05f4ea565d523f023d78d90193e36e6460ffe1ae /src
parent762f9c005d5676459d0a8d542133fa636be86032 (diff)
downloadpianobar-windows-cc2e11de05ffce5043ee19cfbe33fdddef3e5b5a.tar.gz
pianobar-windows-cc2e11de05ffce5043ee19cfbe33fdddef3e5b5a.tar.bz2
pianobar-windows-cc2e11de05ffce5043ee19cfbe33fdddef3e5b5a.zip
Simplify readline
Inspired by pianobar-windows
Diffstat (limited to 'src')
-rw-r--r--src/ui_readline.c56
1 files changed, 18 insertions, 38 deletions
diff --git a/src/ui_readline.c b/src/ui_readline.c
index c05cc8f..6509f4e 100644
--- a/src/ui_readline.c
+++ b/src/ui_readline.c
@@ -29,27 +29,17 @@ THE SOFTWARE.
#include "ui_readline.h"
-static inline void BarReadlineMoveLeft (char *buf, size_t *bufPos,
- size_t *bufLen) {
- char *tmpBuf = &buf[*bufPos-1];
- while (tmpBuf < &buf[*bufLen]) {
- *tmpBuf = *(tmpBuf+1);
- ++tmpBuf;
- }
- --(*bufPos);
- --(*bufLen);
-}
+/* return size of previous UTF-8 character
+ */
+static size_t BarReadlinePrevUtf8 (char *ptr) {
+ size_t i = 0;
-static inline char BarReadlineIsAscii (char b) {
- return !(b & (1 << 7));
-}
+ do {
+ ++i;
+ --ptr;
+ } while ((*ptr & (1 << 7)) && !(*ptr & (1 << 6)));
-static inline char BarReadlineIsUtf8Start (char b) {
- return (b & (1 << 7)) && (b & (1 << 6));
-}
-
-static inline char BarReadlineIsUtf8Content (char b) {
- return (b & (1 << 7)) && !(b & (1 << 6));
+ return i;
}
/* readline replacement
@@ -112,12 +102,6 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
switch (chr) {
/* EOT */
case 4:
- if (echo) {
- fputs ("\n", stdout);
- }
- return bufLen;
- break;
-
/* return */
case 10:
if (echo) {
@@ -139,19 +123,15 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
case 8: /* ASCII BS */
case 127: /* ASCII DEL */
if (bufPos > 0) {
- if (BarReadlineIsAscii (buf[bufPos-1])) {
- BarReadlineMoveLeft (buf, &bufPos, &bufLen);
- } else {
- /* delete utf-8 multibyte chars */
- /* char content */
- while (BarReadlineIsUtf8Content (buf[bufPos-1])) {
- BarReadlineMoveLeft (buf, &bufPos, &bufLen);
- }
- /* char length */
- if (BarReadlineIsUtf8Start (buf[bufPos-1])) {
- BarReadlineMoveLeft (buf, &bufPos, &bufLen);
- }
- }
+ size_t moveSize = BarReadlinePrevUtf8 (&buf[bufPos]);
+ assert ((signed int) bufPos - (signed int) moveSize >= 0);
+ memmove (&buf[bufPos-moveSize], &buf[bufPos], moveSize);
+
+ bufPos -= moveSize;
+ bufLen -= moveSize;
+
+ buf[bufLen] = '\0';
+
/* move caret back and delete last character */
if (echo) {
fputs ("\033[D\033[K", stdout);