1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 18:23:28 +01:00

refactor string funcs

This commit is contained in:
Jfreegman 2014-06-21 17:55:01 -04:00
parent 62239a1fda
commit 6b9ef7e6c9
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -26,6 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <wchar.h>
#include "toxic.h" #include "toxic.h"
#include "windows.h" #include "windows.h"
@ -38,14 +39,9 @@ void add_char_to_buf(ChatContext *ctx, wint_t ch)
if (ctx->pos < 0 || ctx->len >= MAX_STR_SIZE) if (ctx->pos < 0 || ctx->len >= MAX_STR_SIZE)
return; return;
/* move all chars including null in front of pos one space forward and insert char in pos */ wmemmove(&ctx->line[ctx->pos + 1], &ctx->line[ctx->pos], ctx->len - ctx->pos);
int i;
for (i = ctx->len; i >= ctx->pos && i >= 0; --i)
ctx->line[i + 1] = ctx->line[i];
ctx->line[ctx->pos++] = ch; ctx->line[ctx->pos++] = ch;
++ctx->len; ctx->line[++ctx->len] = L'\0';
} }
/* Deletes the character before pos */ /* Deletes the character before pos */
@ -54,14 +50,9 @@ void del_char_buf_bck(ChatContext *ctx)
if (ctx->pos <= 0) if (ctx->pos <= 0)
return; return;
int i; wmemmove(&ctx->line[ctx->pos - 1], &ctx->line[ctx->pos], ctx->len - ctx->pos);
/* similar to add_char_to_buf but deletes a char */
for (i = ctx->pos - 1; i <= ctx->len; ++i)
ctx->line[i] = ctx->line[i + 1];
--ctx->pos; --ctx->pos;
--ctx->len; ctx->line[--ctx->len] = L'\0';
} }
/* Deletes the character at pos */ /* Deletes the character at pos */
@ -70,12 +61,8 @@ void del_char_buf_frnt(ChatContext *ctx)
if (ctx->pos < 0 || ctx->pos >= ctx->len) if (ctx->pos < 0 || ctx->pos >= ctx->len)
return; return;
int i; wmemmove(&ctx->line[ctx->pos], &ctx->line[ctx->pos + 1], ctx->len - ctx->pos - 1);
ctx->line[--ctx->len] = L'\0';
for (i = ctx->pos; i < ctx->len; ++i)
ctx->line[i] = ctx->line[i + 1];
--ctx->len;
} }
/* Deletes the line from beginning to pos */ /* Deletes the line from beginning to pos */
@ -84,15 +71,11 @@ void discard_buf(ChatContext *ctx)
if (ctx->pos <= 0) if (ctx->pos <= 0)
return; return;
int i; wmemmove(ctx->line, &ctx->line[ctx->pos], ctx->len - ctx->pos);
int c = 0; ctx->len -= ctx->pos;
for (i = ctx->pos; i <= ctx->len; ++i)
ctx->line[c++] = ctx->line[i];
ctx->pos = 0; ctx->pos = 0;
ctx->start = 0; ctx->start = 0;
ctx->len = c - 1; ctx->line[ctx->len] = L'\0';
} }
/* Deletes the line from pos to len */ /* Deletes the line from pos to len */