Browse Source

Introduce ENDS_WITH

rexim 4 years ago
parent
commit
83b8da3a78
5 changed files with 30 additions and 12 deletions
  1. 6 0
      ChangeLog.md
  2. 1 1
      examples/foreach.c
  3. 11 5
      examples/string.c
  4. 2 5
      nobuild.c
  5. 10 1
      nobuild.h

+ 6 - 0
ChangeLog.md

@@ -1,5 +1,11 @@
 [semver](https://semver.org/) is implied.
 
+# 0.2.0-dev
+
+*Not Released Yet*
+
+- Introduced `ENDS_WITH(str, postfix)` function that checks if `str` ends with `postfix`.
+
 # 0.1.0 — Cleaning Things Up. Figuring Out Conventions.
 
 *2021-01-27*

+ 1 - 1
examples/foreach.c

@@ -24,7 +24,7 @@ void foreach_array(void)
 void foreach_file_in_dir(const char *dir_path)
 {
     FOREACH_FILE_IN_DIR(file, dir_path, {
-            INFO("    %s", file);
+        INFO("    %s", file);
     });
 }
 

+ 11 - 5
examples/string.c

@@ -1,14 +1,20 @@
 #define NOBUILD_IMPLEMENTATION
 #include "../nobuild.h"
 
-#define DEMO(expr)                              \
+#define DEMO_S(expr)                         \
     INFO(#expr " == \"%s\"", expr)
 
+#define DEMO_D(expr)                         \
+    INFO(#expr " == %d", expr)
+
 int main(int argc, char *argv[])
 {
-    DEMO(CONCAT("foo", "bar", "baz"));
-    DEMO(PATH("foo", "bar", "baz"));
-    DEMO(JOIN("++", "foo", "bar", "baz"));
-    DEMO(NOEXT("main.c"));
+    DEMO_S(CONCAT("foo", "bar", "baz"));
+    DEMO_S(PATH("foo", "bar", "baz"));
+    DEMO_S(JOIN("++", "foo", "bar", "baz"));
+    DEMO_S(NOEXT("main.c"));
+    DEMO_D(ENDS_WITH("main.c", ".c"));
+    DEMO_D(ENDS_WITH("main.java", ".c"));
+    DEMO_D(ENDS_WITH("", ".c"));
     return 0;
 }

+ 2 - 5
nobuild.c

@@ -19,11 +19,8 @@ void check_example(const char *example)
 int main(int argc, char *argv[])
 {
     FOREACH_FILE_IN_DIR(example, "examples", {
-        if(*example != '.') {
-            size_t n = strlen(example);
-            if (n >= 2 && strcmp(example + n - 2, ".c") == 0) {
-                check_example(example);
-            }
+        if (ENDS_WITH(example, ".c")) {
+            check_example(example);
         }
     });
     return 0;

+ 10 - 1
nobuild.h

@@ -21,7 +21,7 @@
 //
 // ============================================================
 //
-// nobuild — 0.1.0 — Header only library for writing build recipes in C.
+// nobuild — 0.2.0-dev — Header only library for writing build recipes in C.
 //
 // https://github.com/tsoding/nobuild
 //
@@ -157,6 +157,7 @@
 const char *concat_impl(int ignore, ...);
 const char *concat_sep_impl(const char *sep, ...);
 const char *build__join(const char *sep, ...);
+int nobuild__ends_with(const char *str, const char *postfix);
 void mkdirs_impl(int ignore, ...);
 void cmd_impl(int ignore, ...);
 void nobuild_exec(const char **argv);
@@ -169,6 +170,7 @@ char *shift(int *argc, char ***argv);
 #define PATH(...) JOIN(PATH_SEP, __VA_ARGS__)
 #define MKDIRS(...) mkdirs_impl(69, __VA_ARGS__, NULL)
 #define NOEXT(path) nobuild__remove_ext(path)
+#define ENDS_WITH(str, postfix) nobuild__ends_with(str, postfix)
 
 void nobuild_log(FILE *stream, const char *tag, const char *fmt, ...);
 void nobuild_vlog(FILE *stream, const char *tag, const char *fmt, va_list args);
@@ -581,4 +583,11 @@ void ERRO(const char *fmt, ...)
     va_end(args);
 }
 
+int nobuild__ends_with(const char *str, const char *postfix)
+{
+    const size_t str_n = strlen(str);
+    const size_t postfix_n = strlen(postfix);
+    return postfix_n <= str_n && strcmp(str + str_n - postfix_n, postfix) == 0;
+}
+
 #endif // NOBUILD_IMPLEMENTATION