summaryrefslogtreecommitdiffstats
path: root/generic/tclUtil.c
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2020-04-13 13:51:50 (GMT)
committerdgp <dgp@users.sourceforge.net>2020-04-13 13:51:50 (GMT)
commit787517aa7fc113fcd554b0da726b0ba74585812b (patch)
tree0b5778c84a2ec9d3153da5cb199112380b11c6a9 /generic/tclUtil.c
parentd1a1538c301096e5be710310329fa391a61a3b33 (diff)
downloadtcl-787517aa7fc113fcd554b0da726b0ba74585812b.zip
tcl-787517aa7fc113fcd554b0da726b0ba74585812b.tar.gz
tcl-787517aa7fc113fcd554b0da726b0ba74585812b.tar.bz2
TclTrimLeft and TclTrimRight are internal routines. They demand NUL-terminated
arguments. That's a reasonable burden to put on internal callers, and all existing callers already meet it.
Diffstat (limited to 'generic/tclUtil.c')
-rw-r--r--generic/tclUtil.c94
1 files changed, 25 insertions, 69 deletions
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index d40cbeb..b6eebdc 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -1566,8 +1566,8 @@ UtfWellFormedEnd(
*----------------------------------------------------------------------
*/
-static inline int
-TrimRight(
+int
+TclTrimRight(
const char *bytes, /* String to be trimmed... */
int numBytes, /* ...and its length in bytes */
/* Calls to TclUtfToUniChar() in this routine
@@ -1579,6 +1579,11 @@ TrimRight(
{
const char *pp, *p = bytes + numBytes;
+ /* Empty strings -> nothing to do */
+ if ((numBytes == 0) || (numTrim == 0)) {
+ return 0;
+ }
+
/* Outer loop: iterate over string to be trimmed */
do {
Tcl_UniChar ch1;
@@ -1613,37 +1618,6 @@ TrimRight(
return numBytes - (p - bytes);
}
-
-int
-TclTrimRight(
- const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
- const char *trim, /* String of trim characters... */
- int numTrim) /* ...and its length in bytes */
-{
- int res;
- Tcl_DString bytesBuf, trimBuf;
-
- /* Empty strings -> nothing to do */
- if ((numBytes == 0) || (numTrim == 0)) {
- return 0;
- }
-
- Tcl_DStringInit(&bytesBuf);
- Tcl_DStringInit(&trimBuf);
- bytes = UtfWellFormedEnd(&bytesBuf, bytes, numBytes);
- trim = UtfWellFormedEnd(&trimBuf, trim, numTrim);
-
- res = TrimRight(bytes, numBytes, trim, numTrim);
- if (res > numBytes) {
- res = numBytes;
- }
-
- Tcl_DStringFree(&bytesBuf);
- Tcl_DStringFree(&trimBuf);
-
- return res;
-}
/*
*----------------------------------------------------------------------
@@ -1662,15 +1636,24 @@ TclTrimRight(
*----------------------------------------------------------------------
*/
-static inline int
-TrimLeft(
+int
+TclTrimLeft(
const char *bytes, /* String to be trimmed... */
int numBytes, /* ...and its length in bytes */
+ /* Calls to TclUtfToUniChar() in this routine
+ * rely on (bytes[numBytes] == '\0'). */
const char *trim, /* String of trim characters... */
int numTrim) /* ...and its length in bytes */
+ /* Calls to TclUtfToUniChar() in this routine
+ * rely on (trim[numTrim] == '\0'). */
{
const char *p = bytes;
+ /* Empty strings -> nothing to do */
+ if ((numBytes == 0) || (numTrim == 0)) {
+ return 0;
+ }
+
/* Outer loop: iterate over string to be trimmed */
do {
Tcl_UniChar ch1;
@@ -1702,37 +1685,6 @@ TrimLeft(
return p - bytes;
}
-
-int
-TclTrimLeft(
- const char *bytes, /* String to be trimmed... */
- int numBytes, /* ...and its length in bytes */
- const char *trim, /* String of trim characters... */
- int numTrim) /* ...and its length in bytes */
-{
- int res;
- Tcl_DString bytesBuf, trimBuf;
-
- /* Empty strings -> nothing to do */
- if ((numBytes == 0) || (numTrim == 0)) {
- return 0;
- }
-
- Tcl_DStringInit(&bytesBuf);
- Tcl_DStringInit(&trimBuf);
- bytes = UtfWellFormedEnd(&bytesBuf, bytes, numBytes);
- trim = UtfWellFormedEnd(&trimBuf, trim, numTrim);
-
- res = TrimLeft(bytes, numBytes, trim, numTrim);
- if (res > numBytes) {
- res = numBytes;
- }
-
- Tcl_DStringFree(&bytesBuf);
- Tcl_DStringFree(&trimBuf);
-
- return res;
-}
/*
*----------------------------------------------------------------------
@@ -1754,9 +1706,13 @@ int
TclTrim(
const char *bytes, /* String to be trimmed... */
int numBytes, /* ...and its length in bytes */
+ /* Calls in this routine
+ * rely on (bytes[numBytes] == '\0'). */
const char *trim, /* String of trim characters... */
int numTrim, /* ...and its length in bytes */
- int *trimRight) /* Offset from the end of the string. */
+ /* Calls in this routine
+ * rely on (trim[numTrim] == '\0'). */
+ int *trimRight) /* Offset from the end of the string. */
{
int trimLeft;
Tcl_DString bytesBuf, trimBuf;
@@ -1772,7 +1728,7 @@ TclTrim(
bytes = UtfWellFormedEnd(&bytesBuf, bytes, numBytes);
trim = UtfWellFormedEnd(&trimBuf, trim, numTrim);
- trimLeft = TrimLeft(bytes, numBytes, trim, numTrim);
+ trimLeft = TclTrimLeft(bytes, numBytes, trim, numTrim);
if (trimLeft > numBytes) {
trimLeft = numBytes;
}
@@ -1780,7 +1736,7 @@ TclTrim(
/* have to trim yet (first char was already verified within TrimLeft) */
if (numBytes > 1) {
bytes += trimLeft;
- *trimRight = TrimRight(bytes, numBytes, trim, numTrim);
+ *trimRight = TclTrimRight(bytes, numBytes, trim, numTrim);
if (*trimRight > numBytes) {
*trimRight = numBytes;
}