Skip to content

Commit ef19c71

Browse files
authored
run: de-duplicate fmt and format functions and optimize (#11596)
1 parent 053b3f9 commit ef19c71

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

examples/run/run.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,6 @@
3838
}
3939
#endif
4040

41-
GGML_ATTRIBUTE_FORMAT(1, 2)
42-
static std::string fmt(const char * fmt, ...) {
43-
va_list ap;
44-
va_list ap2;
45-
va_start(ap, fmt);
46-
va_copy(ap2, ap);
47-
const int size = vsnprintf(NULL, 0, fmt, ap);
48-
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
49-
std::string buf;
50-
buf.resize(size);
51-
const int size2 = vsnprintf(const_cast<char *>(buf.data()), buf.size() + 1, fmt, ap2);
52-
GGML_ASSERT(size2 == size);
53-
va_end(ap2);
54-
va_end(ap);
55-
56-
return buf;
57-
}
58-
5941
GGML_ATTRIBUTE_FORMAT(1, 2)
6042
static int printe(const char * fmt, ...) {
6143
va_list args;
@@ -525,11 +507,11 @@ class HttpClient {
525507
int secs = static_cast<int>(seconds) % 60;
526508

527509
if (hrs > 0) {
528-
return fmt("%dh %02dm %02ds", hrs, mins, secs);
510+
return string_format("%dh %02dm %02ds", hrs, mins, secs);
529511
} else if (mins > 0) {
530-
return fmt("%dm %02ds", mins, secs);
512+
return string_format("%dm %02ds", mins, secs);
531513
} else {
532-
return fmt("%ds", secs);
514+
return string_format("%ds", secs);
533515
}
534516
}
535517

@@ -544,7 +526,7 @@ class HttpClient {
544526
}
545527
}
546528

547-
return fmt("%.2f %s", dbl_size, suffix[i]);
529+
return string_format("%.2f %s", dbl_size, suffix[i]);
548530
}
549531

550532
static int update_progress(void * ptr, curl_off_t total_to_download, curl_off_t now_downloaded, curl_off_t,
@@ -578,7 +560,9 @@ class HttpClient {
578560
return (now_downloaded_plus_file_size * 100) / total_to_download;
579561
}
580562

581-
static std::string generate_progress_prefix(curl_off_t percentage) { return fmt("%3ld%% |", static_cast<long int>(percentage)); }
563+
static std::string generate_progress_prefix(curl_off_t percentage) {
564+
return string_format("%3ld%% |", static_cast<long int>(percentage));
565+
}
582566

583567
static double calculate_speed(curl_off_t now_downloaded, const std::chrono::steady_clock::time_point & start_time) {
584568
const auto now = std::chrono::steady_clock::now();
@@ -589,9 +573,9 @@ class HttpClient {
589573
static std::string generate_progress_suffix(curl_off_t now_downloaded_plus_file_size, curl_off_t total_to_download,
590574
double speed, double estimated_time) {
591575
const int width = 10;
592-
return fmt("%*s/%*s%*s/s%*s", width, human_readable_size(now_downloaded_plus_file_size).c_str(), width,
593-
human_readable_size(total_to_download).c_str(), width, human_readable_size(speed).c_str(), width,
594-
human_readable_time(estimated_time).c_str());
576+
return string_format("%*s/%*s%*s/s%*s", width, human_readable_size(now_downloaded_plus_file_size).c_str(),
577+
width, human_readable_size(total_to_download).c_str(), width,
578+
human_readable_size(speed).c_str(), width, human_readable_time(estimated_time).c_str());
595579
}
596580

597581
static int calculate_progress_bar_width(const std::string & progress_prefix, const std::string & progress_suffix) {

0 commit comments

Comments
 (0)