Files
Jungfraujoch/gemmi_gph/gemmi/sprintf.hpp
T
leonarski_f fc68a9baed
Build Packages / Unit tests (push) Skipped
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 8m34s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 10m0s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 10m23s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 10m23s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 11m16s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 11m49s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 8m32s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 9m15s
Build Packages / XDS test (durin plugin) (push) Successful in 7m16s
Build Packages / Generate python client (push) Successful in 16s
Build Packages / build:rpm (rocky9) (push) Successful in 10m12s
Build Packages / Create release (push) Skipped
Build Packages / Build documentation (push) Successful in 47s
Build Packages / DIALS test (push) Successful in 10m18s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 5m46s
Build Packages / build:rpm (rocky8) (push) Successful in 1h41m2s
Build Packages / XDS test (neggia plugin) (push) Successful in 1h59m18s
v1.0.0-rc.146 (#56)
This is an UNSTABLE release. The release has significant modifications for data processing - in case of troubles go back to 1.0.0-rc.144.

jfjoch_process: Generate a dedicated file (_process.h5), which can be used as a replacement for the _master.h5 file for a reanalyzed dataset.
jfjoch_process: Improve the performance of scaling and merging, implement on the fly scaling.
jfjoch_writer: All final data analysis results are repopulated in the _master.h5 file.
jfjoch_scale: Dedicated tool for rescaling/merging existing data.
jfjoch_viewer: Fix bugs where pixel labels where displayed on a wrong pixel.

WARNING! Scaling and merging are experimental at the moment, and may not provide reasonable results for the time being.

Reviewed-on: #56
2026-05-28 18:48:35 +02:00

81 lines
2.3 KiB
C++

// Copyright 2017 Global Phasing Ltd.
//
// interface to stb_sprintf: snprintf_z, to_str(float|double)
#ifndef GEMMI_SPRINTF_HPP_
#define GEMMI_SPRINTF_HPP_
#include <string>
#ifdef __has_include
# if __has_include(<charconv>) && !(defined(_MSVC_LANG) && _MSVC_LANG < 201703L)
# include <charconv>
# endif
#endif
#if __cpp_lib_to_chars < 201611L
# include <algorithm> // for min
#endif
#include "fail.hpp" // for GEMMI_DLL
namespace gemmi {
// On MinGW format(printf) doesn't support %zu.
#if (defined(__GNUC__) && !defined(__MINGW32__)) || defined(__clang__)
# define GEMMI_ATTRIBUTE_FORMAT(fmt,va) __attribute__((format(printf,fmt,va)))
#else
# define GEMMI_ATTRIBUTE_FORMAT(fmt,va)
#endif
/// stb_snprintf in gemmi namespace - like snprintf, but ignores locale
/// and is always zero-terminated (hence _z).
GEMMI_DLL int snprintf_z(char *buf, int count, char const *fmt, ...)
GEMMI_ATTRIBUTE_FORMAT(3,4);
/// stb_sprintf in gemmi namespace
GEMMI_DLL int sprintf_z(char *buf, char const *fmt, ...) GEMMI_ATTRIBUTE_FORMAT(2,3);
inline std::string to_str(double d) {
char buf[24];
int len = sprintf_z(buf, "%.9g", d);
return std::string(buf, len > 0 ? len : 0);
}
inline std::string to_str(float d) {
char buf[16];
int len = sprintf_z(buf, "%.6g", d);
return std::string(buf, len > 0 ? len : 0);
}
template<int Prec>
std::string to_str_prec(double d) {
static_assert(Prec >= 0 && Prec < 7, "unsupported precision");
char buf[16];
int len = d > -1e8 && d < 1e8 ? sprintf_z(buf, "%.*f", Prec, d)
: sprintf_z(buf, "%g", d);
return std::string(buf, len > 0 ? len : 0);
}
/// zero-terminated to_chars()
inline char* to_chars_z(char* first, char* last, int value) {
#if __cpp_lib_to_chars >= 201611L
auto result = std::to_chars(first, last-1, value);
*result.ptr = '\0';
return result.ptr;
#else
int n = snprintf_z(first, int(last - first), "%d", value);
return std::min(first + n, last - 1);
#endif
}
inline char* to_chars_z(char* first, char* last, size_t value) {
#if __cpp_lib_to_chars >= 201611L
auto result = std::to_chars(first, last-1, value);
*result.ptr = '\0';
return result.ptr;
#else
int n = snprintf_z(first, int(last - first), "%zu", value);
return std::min(first + n, last - 1);
#endif
}
} // namespace gemmi
#endif