From: j8takagi Date: Mon, 11 Mar 2019 01:59:08 +0000 (+0900) Subject: 文字列を逆順にするstrrev関数を定義 X-Git-Tag: v0.5p15 X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=commitdiff_plain;h=966fe49d988cbc63e0b58ca02514d89ce8f0bd1f 文字列を逆順にするstrrev関数を定義 --- diff --git a/VERSION b/VERSION index 2609bed..e2f238b 100644 --- a/VERSION +++ b/VERSION @@ -1,2 +1,2 @@ -v0.5p14 +v0.5p15 diff --git a/src/cmem.c b/src/cmem.c index b536895..bd6328d 100644 --- a/src/cmem.c +++ b/src/cmem.c @@ -25,9 +25,7 @@ void *calloc_chk(size_t nmemb, size_t size, const char *tag) char *strdup_chk(const char *s, const char *tag) { assert(s != NULL); - char *t = NULL; - - t = malloc_chk(strlen(s) + 1, tag); + char *t = malloc_chk(strlen(s) + 1, tag); strcpy(t, s); return t; } @@ -70,4 +68,15 @@ void strip_casl2_comment(char *s) } } +char *strrev(const char *s) +{ + char *t = strdup_chk(s, "strrev.t"); + int l = strlen(t); + + for(int i = 0; i < l-1-i; i++) { + char tmp = t[i]; + t[i] = t[l-1-i]; + t[l-1-i] = tmp; + } + return t; } diff --git a/src/word.c b/src/word.c index 4f42d62..3ad9a2b 100644 --- a/src/word.c +++ b/src/word.c @@ -104,19 +104,17 @@ char *word2n(WORD word) enum { MAXLEN = 5, /* WORD値を10進数で表したときの最大けた数 */ }; - char *n = malloc_chk(MAXLEN + 1, "word2n.n"); + char *s = malloc_chk(MAXLEN + 1, "word2n.n"); + char *t = NULL; int d = 0; /* けた数 */ do{ - n[d++] = word % 10 + '0'; + s[d++] = word % 10 + '0'; } while((word /= 10) > 0); - for(int i = 0; i < d; i++) { - char tmp = n[i]; - n[i] = n[(d-1)-i]; - n[(d-1)-i] = tmp; - } - n[d] = '\0'; - return n; + s[d] = '\0'; + t = strrev(s); + FREE(s); + return t; } char *word2bit(const WORD word) @@ -138,14 +136,14 @@ char *word2bit(const WORD word) void print_dumpword(WORD word, bool logicalmode) { - char *bit = NULL; + char *bit = word2bit(word); if(logicalmode == true) { - fprintf(stdout, "%6d", word); + fprintf(stdout, "%6u", word); } else { fprintf(stdout, "%6d", (signed short)word); } - fprintf(stdout, " = #%04X = %s", word, (bit = word2bit(word))); + fprintf(stdout, " = #%04X = %s", word, bit); /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */ if(word >= 0x20 && word <= 0x7E) { fprintf(stdout, " = \'%c\'", word); diff --git a/test/unit/strrev/0.txt b/test/unit/strrev/0.txt new file mode 100644 index 0000000..70aa49e --- /dev/null +++ b/test/unit/strrev/0.txt @@ -0,0 +1,2 @@ +jihgfedcba +edcba diff --git a/test/unit/strrev/Makefile b/test/unit/strrev/Makefile new file mode 100644 index 0000000..27cb772 --- /dev/null +++ b/test/unit/strrev/Makefile @@ -0,0 +1,18 @@ +include ../Define.mk +include ../Test.mk + +CC := gcc +CFLAGS := -Wall -Wextra -g + +CMDSRC_FILE := cmd.c +TARGETDIR := ../../../src +INCLUDEDIR := ../../../include +TESTTARGET_FILES := $(TARGETDIR)/cmem.c # Set test target files + +$(CMD_FILE): $(CMDSRC_FILE) $(TESTTARGET_FILES) + $(CC) $(CFLAGS) -I $(INCLUDEDIR) -o $@ $^ + +clean_cmd: + @rm -rf cmd.dSYM cmd + +clean: clean_cmd diff --git a/test/unit/strrev/cmd.c b/test/unit/strrev/cmd.c new file mode 100644 index 0000000..9c9eba7 --- /dev/null +++ b/test/unit/strrev/cmd.c @@ -0,0 +1,15 @@ +#include +#include "cmem.h" + +int main() +{ + char *src[] = {"abcdefghij", "abcde"}; + char *dst = NULL; + + for(unsigned i = 0; i < ARRAYSIZE(src); i++) { + dst = strrev(src[i]); + puts(dst); + FREE(dst); + } + return 0; +}