• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aremmell / libsir / 1144

18 Oct 2023 05:31AM UTC coverage: 95.477% (+0.06%) from 95.414%
1144

Pull #340

gitlab-ci

web-flow
Merge branch 'master' into johnsonjh/20231017/utils
Pull Request #340: Add in-place string mangling utilities

99 of 99 new or added lines in 2 files covered. (100.0%)

3863 of 4046 relevant lines covered (95.48%)

502910.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/src/sirutils.c
1
/**
2
 * @file sirutils.c
3
 *
4
 * @brief String utility functions
5
 *
6
 * Miscellanous utility functions operating on NULL-terminated strings.
7
 *
8
 * @author    Jeffrey H. Johnson \<trnsz@pobox.com\>
9
 * @date      2018-2023
10
 * @version   2.2.4
11
 * @copyright The MIT License (MIT)
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
14
 * this software and associated documentation files (the "Software"), to deal in
15
 * the Software without restriction, including without limitation the rights to
16
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
17
 * the Software, and to permit persons to whom the Software is furnished to do so,
18
 * subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
26
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
27
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
 */
30
#include "sir/utils.h"
31

32
char* sir_strremove(char *str, const char *sub) {
72✔
33
  if (!str)
72✔
34
      return NULL;
21✔
35

36
  const char* p;
37
  char* r;
38
  char* q;
39

40
  if (!sub)
48✔
41
      return str;
21✔
42

43
  if (*sub && (q = r = strstr(str, sub)) != NULL) {
24✔
44
      size_t len = strnlen(sub, strlen(str));
24✔
45

46
      while ((r = strstr(p = r + len, sub)) != NULL)
24✔
47
          while (p < r)
×
48
              *q++ = *p++;
×
49

50
      while ((*q++ = *p++) != '\0');
48✔
51
  }
52

53
  return str;
21✔
54
}
55

56
char* sir_strsqueeze(char *str) {
48✔
57
  if (!str)
48✔
58
      return NULL;
21✔
59

60
  unsigned long j;
61

62
  for (unsigned long i = j = 0; str[i]; ++i)
792✔
63
      if ((i > 0 && !isspace((unsigned char)(str[i - 1])))
768✔
64
                 || !isspace((unsigned char)(str[i])))
408✔
65
          str[j++] = str[i];
432✔
66

67
  str[j] = '\0';
24✔
68

69
  return str;
24✔
70
}
71

72
char* sir_strredact(char *str, const char *sub, const char c) {
216✔
73
  if (!str)
216✔
74
      return NULL;
21✔
75

76
  if (!sub)
192✔
77
      return str;
21✔
78

79
  char *p = strstr(str, sub);
168✔
80

81
  if (!c || !p)
168✔
82
      return str;
63✔
83

84
  (void)memset(p, c, strnlen(sub, strlen(str)));
96✔
85

86
  return sir_strredact(str, sub, c);
96✔
87
}
88

89
char* sir_strreplace(char *str, const char c, const char n) {
96✔
90
  if (!str)
96✔
91
      return NULL;
21✔
92

93
  char *i = str;
63✔
94

95
  if (!c || !n)
72✔
96
      return str;
42✔
97

98
  while ((i = strchr(i, c)) != NULL)
48✔
99
      *i++ = n;
24✔
100

101
  return str;
21✔
102
}
103

104
size_t sir_strcreplace(char *str, const char c, const char n, int32_t max) {
120✔
105
  char*  i   = str;
105✔
106
  size_t cnt = 0;
105✔
107

108
  if (!str || !c || !n || !max)
120✔
109
      return cnt;
84✔
110

111
  while (cnt < (size_t)max && (i = strchr(i, c)) != NULL) {
48✔
112
      *i++ = n;
24✔
113
      cnt++;
24✔
114
  }
115

116
  return cnt;
21✔
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc