Commit 767e55dab6e5428b214e985614ec6c5ab775aee1

Thomas de Grivel 2024-08-07T16:14:27

file_ext

diff --git a/.ikc3_history b/.ikc3_history
index a8d91ab..dc00a27 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,6 +1,3 @@
-defmodule Socket do
-dlopen("lib/kc3/0.1/http.so")
-def connect = cfn Socket.Buf "socket_buf_init_connect" (Result, Str, Str)
 end
 22
 server = Socket.listen("192.168.1.50
@@ -97,3 +94,6 @@ HTTP.mime_type_load("test/httpd/mime.types")
 HTTP.mime_type("zorglub")
 HTTP.mime_type_def(:zorglub, :application/zorglub)
 HTTP.mime_type("zorglub")
+Str.rindex("abc.def", ".")
+Str.rindex_character("abc.def", '.')
+File.ext("abc.def")
diff --git a/lib/kc3/0.1/file.kc3 b/lib/kc3/0.1/file.kc3
index 44c747c..ff42a05 100644
--- a/lib/kc3/0.1/file.kc3
+++ b/lib/kc3/0.1/file.kc3
@@ -2,6 +2,8 @@ defmodule File do
 
   def exists? = cfn Bool "file_exists" (Str, Result)
 
+  def ext = cfn Str "file_ext" (Str, Result)
+
   def is_directory? = fn (path) do
     sb = File.stat(path)
     if (sb) do
diff --git a/lib/kc3/0.1/str.facts b/lib/kc3/0.1/str.facts
index 197b062..85cf414 100644
--- a/lib/kc3/0.1/str.facts
+++ b/lib/kc3/0.1/str.facts
@@ -2,8 +2,17 @@
   version: 1}
 replace {Str, :is_a, :module}
 replace {Str, :symbol, Str.cast}
-replace {Str.cast, :symbol_value, cfn Str "str_init_cast" (Result, Sym, Tag)}
-add {Str, :symbol, Str.starts_with?}
-replace {Str.starts_with?, :symbol_value, cfn Bool "str_starts_with" (Str, Str)}
+replace {Str.cast, :symbol_value,
+         cfn Str "str_init_cast" (Result, Sym, Tag)}
 add {Str, :symbol, Str.ends_with?}
-replace {Str.ends_with?, :symbol_value, cfn Bool "str_ends_with" (Str, Str)}
+replace {Str.ends_with?, :symbol_value,
+         cfn Bool "str_ends_with" (Str, Str)}
+add {Str, :symbol, Str.rindex_character}
+replace {Str.rindex_character, :symbol_value,
+         cfn Sw "str_rindex_character" (Str, Character)}
+add {Str, :symbol, Str.slice}
+replace {Str.slice, :symbol_value,
+         cfn Str "str_init_slice" (Result, Str, Sw, Sw)}
+add {Str, :symbol, Str.starts_with?}
+replace {Str.starts_with?, :symbol_value,
+         cfn Bool "str_starts_with" (Str, Str)}
diff --git a/libkc3/file.c b/libkc3/file.c
index e890eae..92b6989 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -115,10 +115,10 @@ sw file_copy (const char *from, const char *to)
 
 s_str * file_dirname (const s_str *path, s_str *dest)
 {
-  uw dirsep_pos;
+  sw dirsep_pos;
   assert(path);
   assert(dest);
-  if (! str_rindex_character(path, '/', &dirsep_pos))
+  if ((dirsep_pos = str_rindex_character(path, '/')) < 0)
     return str_init(dest, NULL, 2, "./");
   if (! dirsep_pos)
     return str_init(dest, NULL, 1, "/");
@@ -134,6 +134,16 @@ bool * file_exists (const s_str *path, bool *dest)
   return dest;
 }
 
+s_str * file_ext (const s_str *path, s_str *dest)
+{
+  sw dot_pos;
+  assert(path);
+  assert(dest);
+  if ((dot_pos = str_rindex_character(path, '.')) <= 0)
+    return str_init_empty(dest);
+  return str_init_slice(dest, path, dot_pos + 1, -1);
+}
+
 s_list ** file_list (const s_str *path, s_list **dest)
 {
   DIR           *dir;
diff --git a/libkc3/file.h b/libkc3/file.h
index 55d6d1b..ead39c7 100644
--- a/libkc3/file.h
+++ b/libkc3/file.h
@@ -28,6 +28,7 @@
 bool    file_access (const s_str *path, const s_sym *mode);
 sw      file_copy (const char *from, const char *to);
 s_str * file_dirname (const s_str *path, s_str *dest);
+s_str * file_ext (const s_str *path, s_str *dest);
 s_tag * file_mtime (const s_str *path, s_tag *dest);
 s_str * file_read (const s_str *path, s_str *dest);
 s_str * file_search (const s_str *suffix, const s_sym *mode,
diff --git a/libkc3/str.c b/libkc3/str.c
index 6c6f1a1..4fd602f 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -823,7 +823,7 @@ sw str_read_character_utf8 (s_str *str, character *c)
   return size;
 }
 
-uw * str_rindex_character (const s_str *str, character c, uw *dest)
+sw str_rindex_character (const s_str *str, character c)
 {
   uw i = 0;
   sw result = -1;
@@ -835,11 +835,7 @@ uw * str_rindex_character (const s_str *str, character c, uw *dest)
       result = i;
     i++;
   }
-  if (result >= 0) {
-    *dest = result;
-    return dest;
-  }
-  return NULL;
+  return result;
 }
 
 bool str_starts_with (const s_str *str, const s_str *start)
diff --git a/libkc3/str.h b/libkc3/str.h
index f1d5054..cc95626 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -113,8 +113,7 @@ sw            str_peek_u16 (const s_str *str, u16 *dest);
 sw            str_peek_u32 (const s_str *str, u32 *dest);
 sw            str_peek_u64 (const s_str *str, u64 *dest);
 sw            str_position_1 (const s_str *str, const char *token);
-uw *          str_rindex_character (const s_str *str, character c,
-                                    uw *dest);
+sw            str_rindex_character (const s_str *str, character c);
 bool          str_starts_with (const s_str *str, const s_str *start);
 uw *          str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest);
 s_str *       str_to_hex (const s_str *str, s_str *dest);