aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2025-11-08 11:02:01 +0300
committerMikhail Romanko <me@blankhex.com>2025-11-08 11:02:01 +0300
commit44057e96f33f110cf182ef7df697b21e779e5aae (patch)
tree6c19be88e92179568db442ffe2f77f068fada753
parentd559d3f66b2c81869d292c513c942bb966881244 (diff)
downloadbhlib-44057e96f33f110cf182ef7df697b21e779e5aae.tar.gz
Add string duplication function (strdup)
-rw-r--r--doc/Manual/en/BH_String.pod7
-rw-r--r--doc/Manual/ru/BH_String.pod7
-rw-r--r--include/BH/String.h3
-rw-r--r--src/String/String.c19
4 files changed, 36 insertions, 0 deletions
diff --git a/doc/Manual/en/BH_String.pod b/doc/Manual/en/BH_String.pod
index b9d78d1..c7b027e 100644
--- a/doc/Manual/en/BH_String.pod
+++ b/doc/Manual/en/BH_String.pod
@@ -377,6 +377,13 @@ The optional parameter I<base> specifies the base of the number.
If successful, it returns the converted number or 0.
+=head2 BH_StringDup
+
+ char *BH_StringDup(const char *string);
+
+Creates duplicate of the I<string> (simular to strdup).
+
+
=head1 SEE ALSO
L<BH>
diff --git a/doc/Manual/ru/BH_String.pod b/doc/Manual/ru/BH_String.pod
index bd7f8b3..4e665d5 100644
--- a/doc/Manual/ru/BH_String.pod
+++ b/doc/Manual/ru/BH_String.pod
@@ -368,6 +368,13 @@ I<string> (с ограничением по длинне I<size>).
В случае успеха, возвращает преобразованное число или 0.
+=head2 BH_StringDup
+
+ char *BH_StringDup(const char *string);
+
+Создает копию строки I<string> (схоже с strdup).
+
+
=head1 СМ. ТАКЖЕ
L<BH>
diff --git a/include/BH/String.h b/include/BH/String.h
index 5e1ceb7..4ef0c33 100644
--- a/include/BH/String.h
+++ b/include/BH/String.h
@@ -113,4 +113,7 @@ uint64_t BH_StringToInt64u(const char *string,
int base);
+char *BH_StringDup(const char *string);
+
+
#endif /* BH_STRING_H */
diff --git a/src/String/String.c b/src/String/String.c
new file mode 100644
index 0000000..74f40e2
--- /dev/null
+++ b/src/String/String.c
@@ -0,0 +1,19 @@
+#include <BH/String.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+char *BH_StringDup(const char *string)
+{
+ size_t length;
+ char *result;
+
+ if (!string)
+ return NULL;
+
+ length = strlen(string) + 1;
+ result = malloc(length);
+ if (result)
+ memcpy(result, string, length);
+ return result;
+}