文字列を逆順にするstrrev関数を定義
[YACASL2.git] / src / cmem.c
index e921bb0..bd6328d 100644 (file)
@@ -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;
 }
@@ -47,12 +45,38 @@ char *strndup_chk(const char *s, size_t len, const char *tag)
     return t;
 }
 
-char *strip_end(char *s)
+void strip_end(char *s)
 {
-    int i;
-    i = strlen(s) - 1;
-    while(i > 0 && (s[i] == '\n' || s[i] == ' ' || s[i] == '\t')) {
-        s[i--] = '\0';
+    for(int i = strlen(s) - 1; i > 0 && (s[i] == '\n' || s[i] == '\r' || s[i] == ' ' || s[i] == '\t'); i--) {
+        s[i] = '\0';
+    }
+}
+
+void strip_casl2_comment(char *s)
+{
+    bool quoting = false;
+
+    for(int i = 0; s[i]; i++) {
+        /* 「'」で囲まれた文字列の場合。「''」は無視 */
+        if(s[i] == '\'' && s[i+1] != '\'' && (quoting == false || s[i-1] != '\'')) {
+            quoting = !quoting;
+        /* 「'」で囲まれた文字列でない場合、文字列末尾の「;」以降を削除 */
+        } else if(quoting == false && s[i] == ';') {
+            s[i] = '\0';
+            break;
+        }
     }
-    return 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;
 }