Skip to content
Snippets Groups Projects
Commit 985463d3 authored by Craig Tiller's avatar Craig Tiller
Browse files

Remove sprintf from timeout_encoding

parent c0fc6a1f
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,8 @@ ...@@ -36,6 +36,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <grpc/support/string.h>
static int round_up(int x, int divisor) { static int round_up(int x, int divisor) {
return (x / divisor + (x % divisor != 0)) * divisor; return (x / divisor + (x % divisor != 0)) * divisor;
} }
...@@ -53,15 +55,21 @@ static int round_up_to_three_sig_figs(int x) { ...@@ -53,15 +55,21 @@ static int round_up_to_three_sig_figs(int x) {
} }
/* encode our minimum viable timeout value */ /* encode our minimum viable timeout value */
static void enc_tiny(char *buffer) { strcpy(buffer, "1n"); } static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); }
static void enc_ext(char *buffer, long value, char ext) {
int n = gpr_ltoa(value, buffer);
buffer[n] = ext;
buffer[n+1] = 0;
}
static void enc_seconds(char *buffer, long sec) { static void enc_seconds(char *buffer, long sec) {
if (sec % 3600 == 0) { if (sec % 3600 == 0) {
sprintf(buffer, "%ldH", sec / 3600); enc_ext(buffer, sec / 3600, 'H');
} else if (sec % 60 == 0) { } else if (sec % 60 == 0) {
sprintf(buffer, "%ldM", sec / 60); enc_ext(buffer, sec / 60, 'M');
} else { } else {
sprintf(buffer, "%ldS", sec); enc_ext(buffer, sec, 'S');
} }
} }
...@@ -69,23 +77,23 @@ static void enc_nanos(char *buffer, int x) { ...@@ -69,23 +77,23 @@ static void enc_nanos(char *buffer, int x) {
x = round_up_to_three_sig_figs(x); x = round_up_to_three_sig_figs(x);
if (x < 100000) { if (x < 100000) {
if (x % 1000 == 0) { if (x % 1000 == 0) {
sprintf(buffer, "%du", x / 1000); enc_ext(buffer, x / 1000, 'u');
} else { } else {
sprintf(buffer, "%dn", x); enc_ext(buffer, x, 'n');
} }
} else if (x < 100000000) { } else if (x < 100000000) {
if (x % 1000000 == 0) { if (x % 1000000 == 0) {
sprintf(buffer, "%dm", x / 1000000); enc_ext(buffer, x / 1000000, 'm');
} else { } else {
sprintf(buffer, "%du", x / 1000); enc_ext(buffer, x / 1000, 'u');
} }
} else if (x < 1000000000) { } else if (x < 1000000000) {
sprintf(buffer, "%dm", x / 1000000); enc_ext(buffer, x / 1000000, 'm');
} else { } else {
/* note that this is only ever called with times of less than one second, /* note that this is only ever called with times of less than one second,
so if we reach here the time must have been rounded up to a whole second so if we reach here the time must have been rounded up to a whole second
(and no more) */ (and no more) */
strcpy(buffer, "1S"); memcpy(buffer, "1S", 3);
} }
} }
...@@ -93,18 +101,18 @@ static void enc_micros(char *buffer, int x) { ...@@ -93,18 +101,18 @@ static void enc_micros(char *buffer, int x) {
x = round_up_to_three_sig_figs(x); x = round_up_to_three_sig_figs(x);
if (x < 100000) { if (x < 100000) {
if (x % 1000 == 0) { if (x % 1000 == 0) {
sprintf(buffer, "%dm", x / 1000); enc_ext(buffer, x / 1000, 'm');
} else { } else {
sprintf(buffer, "%du", x); enc_ext(buffer, x, 'u');
} }
} else if (x < 100000000) { } else if (x < 100000000) {
if (x % 1000000 == 0) { if (x % 1000000 == 0) {
sprintf(buffer, "%dS", x / 1000000); enc_ext(buffer, x / 1000000, 'S');
} else { } else {
sprintf(buffer, "%dm", x / 1000); enc_ext(buffer, x / 1000, 'm');
} }
} else { } else {
sprintf(buffer, "%dS", x / 1000000); enc_ext(buffer, x / 1000000, 'S');
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment