1 #include "git-compat-util.h"
5 #include "string-list.h"
9 int starts_with(const char *str
, const char *prefix
)
11 for (; ; str
++, prefix
++)
14 else if (*str
!= *prefix
)
18 int istarts_with(const char *str
, const char *prefix
)
20 for (; ; str
++, prefix
++)
23 else if (tolower(*str
) != tolower(*prefix
))
27 int skip_to_optional_arg_default(const char *str
, const char *prefix
,
28 const char **arg
, const char *def
)
32 if (!skip_prefix(str
, prefix
, &p
))
50 * Used as the default ->buf value, so that people can always assume
51 * buf is non NULL and ->buf is NUL terminated even for a freshly
54 char strbuf_slopbuf
[1];
56 void strbuf_init(struct strbuf
*sb
, size_t hint
)
58 struct strbuf blank
= STRBUF_INIT
;
59 memcpy(sb
, &blank
, sizeof(*sb
));
61 strbuf_grow(sb
, hint
);
64 void strbuf_release(struct strbuf
*sb
)
72 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
83 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
90 sb
->buf
[sb
->len
] = '\0';
93 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
95 int new_buf
= !sb
->alloc
;
96 if (unsigned_add_overflows(extra
, 1) ||
97 unsigned_add_overflows(sb
->len
, extra
+ 1))
98 die("you want to use way too much memory");
101 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
106 void strbuf_trim(struct strbuf
*sb
)
112 void strbuf_rtrim(struct strbuf
*sb
)
114 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
116 sb
->buf
[sb
->len
] = '\0';
119 void strbuf_trim_trailing_dir_sep(struct strbuf
*sb
)
121 while (sb
->len
> 0 && is_dir_sep((unsigned char)sb
->buf
[sb
->len
- 1]))
123 sb
->buf
[sb
->len
] = '\0';
126 void strbuf_trim_trailing_newline(struct strbuf
*sb
)
128 if (sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\n') {
129 if (--sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\r')
131 sb
->buf
[sb
->len
] = '\0';
135 void strbuf_ltrim(struct strbuf
*sb
)
138 while (sb
->len
> 0 && isspace(*b
)) {
142 memmove(sb
->buf
, b
, sb
->len
);
143 sb
->buf
[sb
->len
] = '\0';
146 int strbuf_reencode(struct strbuf
*sb
, const char *from
, const char *to
)
151 if (same_encoding(from
, to
))
154 out
= reencode_string_len(sb
->buf
, sb
->len
, to
, from
, &len
);
158 strbuf_attach(sb
, out
, len
, len
);
162 void strbuf_tolower(struct strbuf
*sb
)
164 char *p
= sb
->buf
, *end
= sb
->buf
+ sb
->len
;
169 struct strbuf
**strbuf_split_buf(const char *str
, size_t slen
,
170 int terminator
, int max
)
172 struct strbuf
**ret
= NULL
;
173 size_t nr
= 0, alloc
= 0;
178 if (max
<= 0 || nr
+ 1 < max
) {
179 const char *end
= memchr(str
, terminator
, slen
);
183 t
= xmalloc(sizeof(struct strbuf
));
185 strbuf_add(t
, str
, len
);
186 ALLOC_GROW(ret
, nr
+ 2, alloc
);
191 ALLOC_GROW(ret
, nr
+ 1, alloc
); /* In case string was empty */
196 void strbuf_add_separated_string_list(struct strbuf
*str
,
198 struct string_list
*slist
)
200 struct string_list_item
*item
;
203 for_each_string_list_item(item
, slist
) {
205 strbuf_addstr(str
, sep
);
206 strbuf_addstr(str
, item
->string
);
211 void strbuf_list_free(struct strbuf
**sbs
)
213 struct strbuf
**s
= sbs
;
224 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
226 size_t len
= a
->len
< b
->len
? a
->len
: b
->len
;
227 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
230 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
233 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
234 const void *data
, size_t dlen
)
236 if (unsigned_add_overflows(pos
, len
))
237 die("you want to use way too much memory");
239 die("`pos' is too far after the end of the buffer");
240 if (pos
+ len
> sb
->len
)
241 die("`pos + len' is too far after the end of the buffer");
244 strbuf_grow(sb
, dlen
- len
);
245 memmove(sb
->buf
+ pos
+ dlen
,
247 sb
->len
- pos
- len
);
248 memcpy(sb
->buf
+ pos
, data
, dlen
);
249 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
252 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
254 strbuf_splice(sb
, pos
, 0, data
, len
);
257 void strbuf_vinsertf(struct strbuf
*sb
, size_t pos
, const char *fmt
, va_list ap
)
264 die("`pos' is too far after the end of the buffer");
266 len
= vsnprintf(sb
->buf
+ sb
->len
, 0, fmt
, cp
);
269 BUG("your vsnprintf is broken (returned %d)", len
);
271 return; /* nothing to do */
272 if (unsigned_add_overflows(sb
->len
, len
))
273 die("you want to use way too much memory");
274 strbuf_grow(sb
, len
);
275 memmove(sb
->buf
+ pos
+ len
, sb
->buf
+ pos
, sb
->len
- pos
);
276 /* vsnprintf() will append a NUL, overwriting one of our characters */
277 save
= sb
->buf
[pos
+ len
];
278 len2
= vsnprintf(sb
->buf
+ pos
, len
+ 1, fmt
, ap
);
279 sb
->buf
[pos
+ len
] = save
;
281 BUG("your vsnprintf is broken (returns inconsistent lengths)");
282 strbuf_setlen(sb
, sb
->len
+ len
);
285 void strbuf_insertf(struct strbuf
*sb
, size_t pos
, const char *fmt
, ...)
289 strbuf_vinsertf(sb
, pos
, fmt
, ap
);
293 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
295 strbuf_splice(sb
, pos
, len
, "", 0);
298 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
300 strbuf_grow(sb
, len
);
301 memcpy(sb
->buf
+ sb
->len
, data
, len
);
302 strbuf_setlen(sb
, sb
->len
+ len
);
305 void strbuf_addbuf(struct strbuf
*sb
, const struct strbuf
*sb2
)
307 strbuf_grow(sb
, sb2
->len
);
308 memcpy(sb
->buf
+ sb
->len
, sb2
->buf
, sb2
->len
);
309 strbuf_setlen(sb
, sb
->len
+ sb2
->len
);
312 const char *strbuf_join_argv(struct strbuf
*buf
,
313 int argc
, const char **argv
, char delim
)
318 strbuf_addstr(buf
, *argv
);
320 strbuf_addch(buf
, delim
);
321 strbuf_addstr(buf
, *(++argv
));
327 void strbuf_addchars(struct strbuf
*sb
, int c
, size_t n
)
330 memset(sb
->buf
+ sb
->len
, c
, n
);
331 strbuf_setlen(sb
, sb
->len
+ n
);
334 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
338 strbuf_vaddf(sb
, fmt
, ap
);
342 static void add_lines(struct strbuf
*out
,
344 const char *buf
, size_t size
,
345 int space_after_prefix
)
348 const char *next
= memchr(buf
, '\n', size
);
349 next
= next
? (next
+ 1) : (buf
+ size
);
351 strbuf_addstr(out
, prefix
);
352 if (space_after_prefix
&& buf
[0] != '\n' && buf
[0] != '\t')
353 strbuf_addch(out
, ' ');
354 strbuf_add(out
, buf
, next
- buf
);
358 strbuf_complete_line(out
);
361 void strbuf_add_commented_lines(struct strbuf
*out
, const char *buf
,
362 size_t size
, const char *comment_prefix
)
364 add_lines(out
, comment_prefix
, buf
, size
, 1);
367 void strbuf_commented_addf(struct strbuf
*sb
, const char *comment_prefix
,
368 const char *fmt
, ...)
371 struct strbuf buf
= STRBUF_INIT
;
372 int incomplete_line
= sb
->len
&& sb
->buf
[sb
->len
- 1] != '\n';
374 va_start(params
, fmt
);
375 strbuf_vaddf(&buf
, fmt
, params
);
378 strbuf_add_commented_lines(sb
, buf
.buf
, buf
.len
, comment_prefix
);
380 sb
->buf
[--sb
->len
] = '\0';
382 strbuf_release(&buf
);
385 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
390 if (!strbuf_avail(sb
))
393 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
396 BUG("your vsnprintf is broken (returned %d)", len
);
397 if (len
> strbuf_avail(sb
)) {
398 strbuf_grow(sb
, len
);
399 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
400 if (len
> strbuf_avail(sb
))
401 BUG("your vsnprintf is broken (insatiable)");
403 strbuf_setlen(sb
, sb
->len
+ len
);
406 int strbuf_expand_step(struct strbuf
*sb
, const char **formatp
)
408 const char *format
= *formatp
;
409 const char *percent
= strchrnul(format
, '%');
411 strbuf_add(sb
, format
, percent
- format
);
414 *formatp
= percent
+ 1;
418 size_t strbuf_expand_literal(struct strbuf
*sb
, const char *placeholder
)
422 switch (placeholder
[0]) {
423 case 'n': /* newline */
424 strbuf_addch(sb
, '\n');
427 /* %x00 == NUL, %x0a == LF, etc. */
428 ch
= hex2chr(placeholder
+ 1);
431 strbuf_addch(sb
, ch
);
437 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
439 size_t i
, len
= src
->len
;
441 for (i
= 0; i
< len
; i
++) {
442 if (src
->buf
[i
] == '%')
443 strbuf_addch(dst
, '%');
444 strbuf_addch(dst
, src
->buf
[i
]);
448 #define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;="
450 void strbuf_add_percentencode(struct strbuf
*dst
, const char *src
, int flags
)
452 size_t i
, len
= strlen(src
);
454 for (i
= 0; i
< len
; i
++) {
455 unsigned char ch
= src
[i
];
456 if (ch
<= 0x1F || ch
>= 0x7F ||
457 (ch
== '/' && (flags
& STRBUF_ENCODE_SLASH
)) ||
458 strchr(URL_UNSAFE_CHARS
, ch
))
459 strbuf_addf(dst
, "%%%02X", (unsigned char)ch
);
461 strbuf_addch(dst
, ch
);
465 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
468 size_t oldalloc
= sb
->alloc
;
470 strbuf_grow(sb
, size
);
471 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
473 strbuf_setlen(sb
, sb
->len
+ res
);
474 else if (oldalloc
== 0)
479 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
481 size_t oldlen
= sb
->len
;
482 size_t oldalloc
= sb
->alloc
;
484 strbuf_grow(sb
, hint
? hint
: 8192);
486 ssize_t want
= sb
->alloc
- sb
->len
- 1;
487 ssize_t got
= read_in_full(fd
, sb
->buf
+ sb
->len
, want
);
493 strbuf_setlen(sb
, oldlen
);
499 strbuf_grow(sb
, 8192);
502 sb
->buf
[sb
->len
] = '\0';
503 return sb
->len
- oldlen
;
506 ssize_t
strbuf_read_once(struct strbuf
*sb
, int fd
, size_t hint
)
508 size_t oldalloc
= sb
->alloc
;
511 strbuf_grow(sb
, hint
? hint
: 8192);
512 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
514 strbuf_setlen(sb
, sb
->len
+ cnt
);
515 else if (oldalloc
== 0)
520 ssize_t
strbuf_write(struct strbuf
*sb
, FILE *f
)
522 return sb
->len
? fwrite(sb
->buf
, 1, sb
->len
, f
) : 0;
525 #define STRBUF_MAXLINK (2*PATH_MAX)
527 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
529 size_t oldalloc
= sb
->alloc
;
534 while (hint
< STRBUF_MAXLINK
) {
537 strbuf_grow(sb
, hint
);
538 len
= readlink(path
, sb
->buf
, hint
);
542 } else if (len
< hint
) {
543 strbuf_setlen(sb
, len
);
547 /* .. the buffer was too small - try again */
555 int strbuf_getcwd(struct strbuf
*sb
)
557 size_t oldalloc
= sb
->alloc
;
558 size_t guessed_len
= 128;
560 for (;; guessed_len
*= 2) {
561 strbuf_grow(sb
, guessed_len
);
562 if (getcwd(sb
->buf
, sb
->alloc
)) {
563 strbuf_setlen(sb
, strlen(sb
->buf
));
568 * If getcwd(3) is implemented as a syscall that falls
569 * back to a regular lookup using readdir(3) etc. then
570 * we may be able to avoid EACCES by providing enough
571 * space to the syscall as it's not necessarily bound
572 * to the same restrictions as the fallback.
574 if (errno
== EACCES
&& guessed_len
< PATH_MAX
)
588 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
597 /* Translate slopbuf to NULL, as we cannot call realloc on it */
601 r
= getdelim(&sb
->buf
, &sb
->alloc
, term
, fp
);
610 * Normally we would have called xrealloc, which will try to free
611 * memory and recover. But we have no way to tell getdelim() to do so.
612 * Worse, we cannot try to recover ENOMEM ourselves, because we have
613 * no idea how many bytes were read by getdelim.
615 * Dying here is reasonable. It mirrors what xrealloc would do on
616 * catastrophic memory failure. We skip the opportunity to free pack
617 * memory and retry, but that's unlikely to help for a malloc small
618 * enough to hold a single line of input, anyway.
621 die("Out of memory, getdelim failed");
624 * Restore strbuf invariants; if getdelim left us with a NULL pointer,
625 * we can just re-init, but otherwise we should make sure that our
626 * length is empty, and that the result is NUL-terminated.
635 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
644 while ((ch
= getc_unlocked(fp
)) != EOF
) {
645 if (!strbuf_avail(sb
))
647 sb
->buf
[sb
->len
++] = ch
;
652 if (ch
== EOF
&& sb
->len
== 0)
655 sb
->buf
[sb
->len
] = '\0';
660 int strbuf_appendwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
662 struct strbuf line
= STRBUF_INIT
;
663 if (strbuf_getwholeline(&line
, fp
, term
))
665 strbuf_addbuf(sb
, &line
);
666 strbuf_release(&line
);
670 static int strbuf_getdelim(struct strbuf
*sb
, FILE *fp
, int term
)
672 if (strbuf_getwholeline(sb
, fp
, term
))
674 if (sb
->buf
[sb
->len
- 1] == term
)
675 strbuf_setlen(sb
, sb
->len
- 1);
679 int strbuf_getdelim_strip_crlf(struct strbuf
*sb
, FILE *fp
, int term
)
681 if (strbuf_getwholeline(sb
, fp
, term
))
683 if (term
== '\n' && sb
->buf
[sb
->len
- 1] == '\n') {
684 strbuf_setlen(sb
, sb
->len
- 1);
685 if (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\r')
686 strbuf_setlen(sb
, sb
->len
- 1);
691 int strbuf_getline(struct strbuf
*sb
, FILE *fp
)
693 return strbuf_getdelim_strip_crlf(sb
, fp
, '\n');
696 int strbuf_getline_lf(struct strbuf
*sb
, FILE *fp
)
698 return strbuf_getdelim(sb
, fp
, '\n');
701 int strbuf_getline_nul(struct strbuf
*sb
, FILE *fp
)
703 return strbuf_getdelim(sb
, fp
, '\0');
706 int strbuf_getwholeline_fd(struct strbuf
*sb
, int fd
, int term
)
712 ssize_t len
= xread(fd
, &ch
, 1);
715 strbuf_addch(sb
, ch
);
722 ssize_t
strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
728 fd
= open(path
, O_RDONLY
);
731 len
= strbuf_read(sb
, fd
, hint
);
742 void strbuf_add_lines(struct strbuf
*out
, const char *prefix
,
743 const char *buf
, size_t size
)
745 add_lines(out
, prefix
, buf
, size
, 0);
748 void strbuf_addstr_xml_quoted(struct strbuf
*buf
, const char *s
)
751 size_t len
= strcspn(s
, "\"<>&");
752 strbuf_add(buf
, s
, len
);
756 strbuf_addstr(buf
, """);
759 strbuf_addstr(buf
, "<");
762 strbuf_addstr(buf
, ">");
765 strbuf_addstr(buf
, "&");
774 static void strbuf_add_urlencode(struct strbuf
*sb
, const char *s
, size_t len
,
775 char_predicate allow_unencoded_fn
)
777 strbuf_grow(sb
, len
);
780 if (allow_unencoded_fn(ch
))
781 strbuf_addch(sb
, ch
);
783 strbuf_addf(sb
, "%%%02x", (unsigned char)ch
);
787 void strbuf_addstr_urlencode(struct strbuf
*sb
, const char *s
,
788 char_predicate allow_unencoded_fn
)
790 strbuf_add_urlencode(sb
, s
, strlen(s
), allow_unencoded_fn
);
793 static void strbuf_humanise(struct strbuf
*buf
, off_t bytes
,
796 if (bytes
> 1 << 30) {
799 /* TRANSLATORS: IEC 80000-13:2008 gibibyte */
801 /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
803 (unsigned)(bytes
>> 30),
804 (unsigned)(bytes
& ((1 << 30) - 1)) / 10737419);
805 } else if (bytes
> 1 << 20) {
806 unsigned x
= bytes
+ 5243; /* for rounding */
809 /* TRANSLATORS: IEC 80000-13:2008 mebibyte */
811 /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
813 x
>> 20, ((x
& ((1 << 20) - 1)) * 100) >> 20);
814 } else if (bytes
> 1 << 10) {
815 unsigned x
= bytes
+ 5; /* for rounding */
818 /* TRANSLATORS: IEC 80000-13:2008 kibibyte */
820 /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
822 x
>> 10, ((x
& ((1 << 10) - 1)) * 100) >> 10);
826 /* TRANSLATORS: IEC 80000-13:2008 byte */
827 Q_("%u byte", "%u bytes", bytes
) :
828 /* TRANSLATORS: IEC 80000-13:2008 byte/second */
829 Q_("%u byte/s", "%u bytes/s", bytes
),
834 void strbuf_humanise_bytes(struct strbuf
*buf
, off_t bytes
)
836 strbuf_humanise(buf
, bytes
, 0);
839 void strbuf_humanise_rate(struct strbuf
*buf
, off_t bytes
)
841 strbuf_humanise(buf
, bytes
, 1);
844 int printf_ln(const char *fmt
, ...)
849 ret
= vprintf(fmt
, ap
);
851 if (ret
< 0 || putchar('\n') == EOF
)
856 int fprintf_ln(FILE *fp
, const char *fmt
, ...)
861 ret
= vfprintf(fp
, fmt
, ap
);
863 if (ret
< 0 || putc('\n', fp
) == EOF
)
868 char *xstrdup_tolower(const char *string
)
873 len
= strlen(string
);
874 result
= xmallocz(len
);
875 for (i
= 0; i
< len
; i
++)
876 result
[i
] = tolower(string
[i
]);
880 char *xstrdup_toupper(const char *string
)
885 len
= strlen(string
);
886 result
= xmallocz(len
);
887 for (i
= 0; i
< len
; i
++)
888 result
[i
] = toupper(string
[i
]);
892 char *xstrvfmt(const char *fmt
, va_list ap
)
894 struct strbuf buf
= STRBUF_INIT
;
895 strbuf_vaddf(&buf
, fmt
, ap
);
896 return strbuf_detach(&buf
, NULL
);
899 char *xstrfmt(const char *fmt
, ...)
905 ret
= xstrvfmt(fmt
, ap
);
911 void strbuf_addftime(struct strbuf
*sb
, const char *fmt
, const struct tm
*tm
,
912 int tz_offset
, int suppress_tz_name
)
914 struct strbuf munged_fmt
= STRBUF_INIT
;
922 * There is no portable way to pass timezone information to
923 * strftime, so we handle %z and %Z here. Likewise '%s', because
924 * going back to an epoch time requires knowing the zone.
926 * Note that tz_offset is in the "[-+]HHMM" decimal form; this is what
927 * we want for %z, but the computation for %s has to convert to number
930 while (strbuf_expand_step(&munged_fmt
, &fmt
)) {
931 if (skip_prefix(fmt
, "%", &fmt
))
932 strbuf_addstr(&munged_fmt
, "%%");
933 else if (skip_prefix(fmt
, "s", &fmt
))
934 strbuf_addf(&munged_fmt
, "%"PRItime
,
935 (timestamp_t
)tm_to_time_t(tm
) -
936 3600 * (tz_offset
/ 100) -
937 60 * (tz_offset
% 100));
938 else if (skip_prefix(fmt
, "z", &fmt
))
939 strbuf_addf(&munged_fmt
, "%+05d", tz_offset
);
940 else if (suppress_tz_name
&& skip_prefix(fmt
, "Z", &fmt
))
943 strbuf_addch(&munged_fmt
, '%');
945 fmt
= munged_fmt
.buf
;
947 strbuf_grow(sb
, hint
);
948 len
= strftime(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, tm
);
952 * strftime reports "0" if it could not fit the result in the buffer.
953 * Unfortunately, it also reports "0" if the requested time string
954 * takes 0 bytes. So our strategy is to munge the format so that the
955 * output contains at least one character, and then drop the extra
956 * character before returning.
958 strbuf_addch(&munged_fmt
, ' ');
961 strbuf_grow(sb
, hint
);
962 len
= strftime(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
,
965 len
--; /* drop munged space */
967 strbuf_release(&munged_fmt
);
968 strbuf_setlen(sb
, sb
->len
+ len
);
972 * Returns the length of a line, without trailing spaces.
974 * If the line ends with newline, it will be removed too.
976 static size_t cleanup(char *line
, size_t len
)
979 unsigned char c
= line
[len
- 1];
989 * Remove empty lines from the beginning and end
990 * and also trailing spaces from every line.
992 * Turn multiple consecutive empty lines between paragraphs
993 * into just one empty line.
995 * If the input has only empty lines and spaces,
996 * no output will be produced.
998 * If last line does not have a newline at the end, one is added.
1000 * Pass a non-NULL comment_prefix to skip every line starting
1003 void strbuf_stripspace(struct strbuf
*sb
, const char *comment_prefix
)
1006 size_t i
, j
, len
, newlen
;
1009 /* We may have to add a newline. */
1012 for (i
= j
= 0; i
< sb
->len
; i
+= len
, j
+= newlen
) {
1013 eol
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
1014 len
= eol
? eol
- (sb
->buf
+ i
) + 1 : sb
->len
- i
;
1016 if (comment_prefix
&& len
&&
1017 starts_with(sb
->buf
+ i
, comment_prefix
)) {
1021 newlen
= cleanup(sb
->buf
+ i
, len
);
1023 /* Not just an empty line? */
1025 if (empties
> 0 && j
> 0)
1026 sb
->buf
[j
++] = '\n';
1028 memmove(sb
->buf
+ j
, sb
->buf
+ i
, newlen
);
1029 sb
->buf
[newlen
+ j
++] = '\n';
1035 strbuf_setlen(sb
, j
);
1038 void strbuf_strip_file_from_path(struct strbuf
*sb
)
1040 char *path_sep
= find_last_dir_sep(sb
->buf
);
1041 strbuf_setlen(sb
, path_sep
? path_sep
- sb
->buf
+ 1 : 0);