commit f192036611bee5e17dcb2de39723e205605e9b68
parent 9a5ea0acee535040934cef883d3fa4916eec6906
Author: Remy Noulin <loader2x@gmail.com>
Date: Mon, 6 Mar 2023 14:28:14 +0200
add cleanFd and replaceSLen
release/json/libsheepyCSmallString.h | 3 +-
release/libsheepy.c | 63 ++++++++++++++++++++++++++++++++++++
release/libsheepy.h | 16 +++++++--
src/json/libsheepyCSmallString.h | 3 +-
src/libsheepy.c | 63 ++++++++++++++++++++++++++++++++++++
src/libsheepy.h | 16 +++++++--
6 files changed, 156 insertions(+), 8 deletions(-)
Diffstat:
6 files changed, 156 insertions(+), 8 deletions(-)
diff --git a/release/json/libsheepyCSmallString.h b/release/json/libsheepyCSmallString.h
@@ -611,8 +611,7 @@ typedef char (*cropElemSmallStringFt)(smallStringt *self, int64_t index);
/**
- * copy string between start and end
- * self becomes the smallString between start and end
+ * copy string between start and end in a new smallString object
* negative indexes are allowed
*
* \param
diff --git a/release/libsheepy.c b/release/libsheepy.c
@@ -82,6 +82,7 @@ internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void *
void cleanUpCharFree(char **val);
void cleanUpListFree(char* **val);
void cleanUpFileFree(FILE **val);
+void cleanUpFd(int *val);
uint64_t shStopwatch(uint8_t op);
int getLogSymbols(void);
void setLogSymbols(int mode);
@@ -285,6 +286,7 @@ char* replaceS(const char *s, const char *olds, const char *news, size_t max );
char *replaceCharSS(const char *s, char olds, const char *news, size_t max);
char *replaceSCharS(const char *s, const char *olds, char news, size_t max);
char *replaceCharCharS(const char *s, char olds, char news, size_t max);
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max);
char* iReplaceS(char **s, const char *olds, const char *news, size_t max );
#define iReplaceS_max(s,olds,news) iReplaceS(s,olds,news, 0)
char *iReplaceCharSS(char **s, char olds, const char *news, size_t max);
@@ -933,6 +935,14 @@ void cleanUpFileFree(FILE **val) {
}
}
+
+void cleanUpFd(int *val) {
+
+ if ((*val != -1)) {
+ close(*val);
+}
+ }
+
/**
* nanosecond stopwatch
*
@@ -7811,6 +7821,59 @@ char *replaceCharCharS(const char *s, char olds, char news, size_t max) {
return(replaceS(s, p, n, max));
}
+/**
+ * replaceSLen returns the length of the resulting string
+ */
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max) {
+ char *tmp = NULL;
+ // ins is next insert point
+ const char *ins = NULL;
+ size_t count;
+
+ // sanity checks and initialization
+ // count the number of replacements needed
+ // allocate result
+ // replace olds with news
+ // copy end of string
+
+ // sanity checks and initialization
+ if (!s) {
+ return(0);
+ }
+ if (isEmptyS(s)) {
+ // return "" when s is empty
+ return(0);
+ }
+ if (!olds) {
+ return(0);
+ }
+ size_t lolds;
+ lolds = strlen(olds);
+ if (!lolds) {
+ // empty olds causes infinite loop
+ return(0);
+ }
+ if (!news) {
+ // empty string for NULL
+ news = "";
+ }
+
+ // count the number of replacements needed
+ ins = s;
+ for (count = 0 ; (tmp = strstr(ins, olds)) ; ++count) {
+ ins = tmp + lolds;
+ if (max && (count == max)) {
+ // the maximum number is replacements is reached, stop
+ break;
+ }
+ }
+
+
+ size_t lnews;
+ lnews = strlen(news);
+
+ return(strlen(s) + (lnews - lolds) * count);
+}
/**
* replace String
diff --git a/release/libsheepy.h b/release/libsheepy.h
@@ -98,7 +98,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "2.2.10.3"
+#define LIBSHEEPY_VERSION "2.2.11"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME
@@ -725,10 +725,19 @@ void cleanUpListFree(char*** val);
void cleanUpFileFree(FILE **val);
/**
- * declare pointer name with type FILE and free name when it is out of scope
+ * declare pointer name with type FILE and free (close) name when it is out of scope
*/
#define cleanFileP(name) FILE *name CLEANUP(cleanUpFileFree)
+// close val when it is out of scope
+void cleanUpFd(int *val);
+
+/**
+ * declare a file descriptor name and close name when it is out of scope
+ */
+#define cleanFd(name) int name CLEANUP(cleanUpFd)
+#define cleanFdInit(name) int name CLEANUP(cleanUpFd) = -1
+
/**
* log variable and its value
*
@@ -1948,6 +1957,9 @@ char *replaceSCharS(const char *s, const char *olds, char news, size_t max) MUST
char *replaceCharCharS(const char *s, char olds, char news, size_t max) MUST_CHECK;
#define replaceS_max(s,olds,news) replaceS(s,olds,news, 0)
#define replaceSMax replaceS_max
+// TODO add support for all types, create a generic, create ignore case version
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max) MUST_CHECK;
+#define replaceSMaxLen(s,olds,news) replaceSLen(s,olds,news, 0)
char* iReplaceS(char **s, const char *olds, const char *news, size_t max) MUST_CHECK;
char* iReplaceCharSS(char **s, char olds, const char *news, size_t max) MUST_CHECK;
char* iReplaceSCharS(char **s, const char *olds, char news, size_t max) MUST_CHECK;
diff --git a/src/json/libsheepyCSmallString.h b/src/json/libsheepyCSmallString.h
@@ -611,8 +611,7 @@ typedef char (*cropElemSmallStringFt)(smallStringt *self, int64_t index);
/**
- * copy string between start and end
- * self becomes the smallString between start and end
+ * copy string between start and end in a new smallString object
* negative indexes are allowed
*
* \param
diff --git a/src/libsheepy.c b/src/libsheepy.c
@@ -84,6 +84,7 @@ internal void segfault_sigaction(int signal UNUSED, siginfo_t *si UNUSED, void *
void cleanUpCharFree(char **val);
void cleanUpListFree(char* **val);
void cleanUpFileFree(FILE **val);
+void cleanUpFd(int *val);
uint64_t shStopwatch(uint8_t op);
int getLogSymbols(void);
void setLogSymbols(int mode);
@@ -287,6 +288,7 @@ char* replaceS(const char *s, const char *olds, const char *news, size_t max );
char *replaceCharSS(const char *s, char olds, const char *news, size_t max);
char *replaceSCharS(const char *s, const char *olds, char news, size_t max);
char *replaceCharCharS(const char *s, char olds, char news, size_t max);
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max);
char* iReplaceS(char **s, const char *olds, const char *news, size_t max );
#define iReplaceS_max(s,olds,news) iReplaceS(s,olds,news, 0)
char *iReplaceCharSS(char **s, char olds, const char *news, size_t max);
@@ -989,6 +991,14 @@ void cleanUpFileFree(FILE **val) {
}
}
+
+void cleanUpFd(int *val) {
+
+ if ((*val != -1)) {
+ close(*val);
+}
+ }
+
/**
* nanosecond stopwatch
*
@@ -7867,6 +7877,59 @@ char *replaceCharCharS(const char *s, char olds, char news, size_t max) {
return(replaceS(s, p, n, max));
}
+/**
+ * replaceSLen returns the length of the resulting string
+ */
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max) {
+ char *tmp = NULL;
+ // ins is next insert point
+ const char *ins = NULL;
+ size_t count;
+
+ // sanity checks and initialization
+ // count the number of replacements needed
+ // allocate result
+ // replace olds with news
+ // copy end of string
+
+ // sanity checks and initialization
+ if (!s) {
+ return(0);
+ }
+ if (isEmptyS(s)) {
+ // return "" when s is empty
+ return(0);
+ }
+ if (!olds) {
+ return(0);
+ }
+ size_t lolds;
+ lolds = strlen(olds);
+ if (!lolds) {
+ // empty olds causes infinite loop
+ return(0);
+ }
+ if (!news) {
+ // empty string for NULL
+ news = "";
+ }
+
+ // count the number of replacements needed
+ ins = s;
+ for (count = 0 ; (tmp = strstr(ins, olds)) ; ++count) {
+ ins = tmp + lolds;
+ if (max && (count == max)) {
+ // the maximum number is replacements is reached, stop
+ break;
+ }
+ }
+
+
+ size_t lnews;
+ lnews = strlen(news);
+
+ return(strlen(s) + (lnews - lolds) * count);
+}
/**
* replace String
diff --git a/src/libsheepy.h b/src/libsheepy.h
@@ -98,7 +98,7 @@
// version accoring to the version package: Release.Major.minor.patch
// https://noulin.net/version/file/README.md.html
-#define LIBSHEEPY_VERSION "2.2.10.3"
+#define LIBSHEEPY_VERSION "2.2.11"
#ifndef SH_PREFIX
#define SH_PREFIX(NAME) NAME
@@ -725,10 +725,19 @@ void cleanUpListFree(char*** val);
void cleanUpFileFree(FILE **val);
/**
- * declare pointer name with type FILE and free name when it is out of scope
+ * declare pointer name with type FILE and free (close) name when it is out of scope
*/
#define cleanFileP(name) FILE *name CLEANUP(cleanUpFileFree)
+// close val when it is out of scope
+void cleanUpFd(int *val);
+
+/**
+ * declare a file descriptor name and close name when it is out of scope
+ */
+#define cleanFd(name) int name CLEANUP(cleanUpFd)
+#define cleanFdInit(name) int name CLEANUP(cleanUpFd) = -1
+
/**
* log variable and its value
*
@@ -1948,6 +1957,9 @@ char *replaceSCharS(const char *s, const char *olds, char news, size_t max) MUST
char *replaceCharCharS(const char *s, char olds, char news, size_t max) MUST_CHECK;
#define replaceS_max(s,olds,news) replaceS(s,olds,news, 0)
#define replaceSMax replaceS_max
+// TODO add support for all types, create a generic, create ignore case version
+size_t replaceSLen(const char *s, const char *olds, const char *news, size_t max) MUST_CHECK;
+#define replaceSMaxLen(s,olds,news) replaceSLen(s,olds,news, 0)
char* iReplaceS(char **s, const char *olds, const char *news, size_t max) MUST_CHECK;
char* iReplaceCharSS(char **s, char olds, const char *news, size_t max) MUST_CHECK;
char* iReplaceSCharS(char **s, const char *olds, char news, size_t max) MUST_CHECK;