summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bdfb3f5)
raw | patch | inline | side by side (parent: bdfb3f5)
author | Florian Forster <octo@collectd.org> | |
Tue, 14 Jan 2014 06:55:02 +0000 (07:55 +0100) | ||
committer | Florian Forster <octo@collectd.org> | |
Tue, 14 Jan 2014 06:55:02 +0000 (07:55 +0100) |
Cleaner and slightly more efficient (3.8% faster) version of escape_slashes().
This function is quite hot with, depending on the configuration, between 1.2%
and 2.6% of total CPU time spent here.
This function is quite hot with, depending on the configuration, between 1.2%
and 2.6% of total CPU time spent here.
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index 161b4d67fc063d693b1f319e016687cea4ae639d..81142fd0084bb4d9c89ea7061c62aca72cd12b7d 100644 (file)
--- a/src/common.c
+++ b/src/common.c
/**
* collectd - src/common.c
- * Copyright (C) 2005-2010 Florian octo Forster
+ * Copyright (C) 2005-2014 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
return (buffer_len);
} /* size_t strstripnewline */
-int escape_slashes (char *buf, int buf_len)
+int escape_slashes (char *buffer, size_t buffer_size)
{
int i;
+ size_t buffer_len;
- if (strcmp (buf, "/") == 0)
- {
- if (buf_len < 5)
- return (-1);
+ buffer_len = strlen (buffer);
- strncpy (buf, "root", buf_len);
+ if (buffer_len <= 1)
+ {
+ if (strcmp ("/", buffer) == 0)
+ {
+ if (buffer_size < 5)
+ return (-1);
+ sstrncpy (buffer, "root", buffer_size);
+ }
return (0);
}
- if (buf_len <= 1)
- return (0);
-
/* Move one to the left */
- if (buf[0] == '/')
- memmove (buf, buf + 1, buf_len - 1);
+ if (buffer[0] == '/')
+ {
+ memmove (buffer, buffer + 1, buffer_len);
+ buffer_len--;
+ }
- for (i = 0; i < buf_len - 1; i++)
+ for (i = 0; i < buffer_len - 1; i++)
{
- if (buf[i] == '\0')
- break;
- else if (buf[i] == '/')
- buf[i] = '_';
+ if (buffer[i] == '/')
+ buffer[i] = '_';
}
- buf[i] = '\0';
return (0);
} /* int escape_slashes */
diff --git a/src/common.h b/src/common.h
index 317be8d1579d063cc23338e229abcdede533da84..67f307c32da05137721adef7028e0fc691a58267 100644 (file)
--- a/src/common.h
+++ b/src/common.h
/**
* collectd - src/common.h
- * Copyright (C) 2005-2010 Florian octo Forster
+ * Copyright (C) 2005-2014 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -158,19 +158,21 @@ int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const
* escape_slashes
*
* DESCRIPTION
- * Removes slashes from the string `buf' and substitutes them with something
- * appropriate. This function should be used whenever a path is to be used as
- * (part of) an instance.
+ * Removes slashes ("/") from "buffer". If buffer contains a single slash,
+ * the result will be "root". Leading slashes are removed. All other slashes
+ * are replaced with underscores ("_").
+ * This function is used by plugin_dispatch_values() to escape all parts of
+ * the identifier.
*
* PARAMETERS
- * `buf' String to be escaped.
- * `buf_len' Length of the buffer. No more then this many bytes will be
- * written to `buf', including the trailing null-byte.
+ * `buffer' String to be escaped.
+ * `buffer_size' Size of the buffer. No more then this many bytes will be
+ * written to `buffer', including the trailing null-byte.
*
* RETURN VALUE
* Returns zero upon success and a value smaller than zero upon failure.
*/
-int escape_slashes (char *buf, int buf_len);
+int escape_slashes (char *buffer, size_t buffer_size);
/*
* NAME