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

aremmell / libsir / 1136

18 Oct 2023 03:21AM UTC coverage: 95.353% (+0.07%) from 95.288%
1136

Pull #340

gitlab-ci

johnsonjh
Whitespace changes

Signed-off-by: Jeffrey H. Johnson <trnsz@pobox.com>
Pull Request #340: Add in-place string mangling utilities

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

3858 of 4046 relevant lines covered (95.35%)

499848.63 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) {
69✔
33
  if (!str)
69✔
34
      return NULL;
20✔
35

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

40
  if (!sub)
46✔
41
      return str;
20✔
42

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

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

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

53
  return str;
20✔
54
}
55

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

60
  unsigned long j;
61

62
  for (unsigned long i = j = 0; str[i]; ++i)
759✔
63
      if ((i > 0 && !isspace((unsigned char)(str[i - 1])))
736✔
64
                 || !isspace((unsigned char)(str[i])))
391✔
65
          str[j++] = str[i];
414✔
66

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

69
  return str;
23✔
70
}
71

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

76
  if (!sub)
184✔
77
      return str;
20✔
78

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

81
  if (!c || !p)
161✔
82
      return str;
60✔
83

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

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

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

93
  char *i = str;
60✔
94

95
  if (!c || !n)
69✔
96
      return str;
40✔
97

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

101
  return str;
20✔
102
}
103

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

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

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

116
  return cnt;
20✔
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