37#define TEMP_STR_SIZE 256
47#define UN_SC_KM 1000.0f
48#define UN_SC_HM 100.0f
49#define UN_SC_DAM 10.0f
53#define UN_SC_MM 0.001f
54#define UN_SC_UM 0.000001f
57#define UN_SC_MI 1609.344f
58#define UN_SC_FUR 201.168f
59#define UN_SC_CH 20.1168f
60#define UN_SC_YD 0.9144f
61#define UN_SC_FT 0.3048f
62#define UN_SC_IN 0.0254f
63#define UN_SC_MIL 0.0000254f
66#define UN_SC_MTON 1000.0f
67#define UN_SC_QL 100.0f
70#define UN_SC_DAG 0.01f
72#define UN_SC_MG 0.000001f
75#define UN_SC_ITON 907.18474f
76#define UN_SC_CWT 45.359237f
77#define UN_SC_ST 6.35029318f
78#define UN_SC_LB 0.45359237f
79#define UN_SC_OZ 0.028349523125f
82#define UN_SC_FAH 0.555555555555f
136#define UNIT_COLLECTION_LENGTH(def) (ARRAY_SIZE(def) - 1)
463 "square hectometers",
466 "Square Hectometers",
508 "square centimeters",
511 "Square Centimeters",
519 "square millimeters",
522 "Square Millimeters",
530 "square micrometers",
533 "Square Micrometers",
997 "kilometer per hour",
998 "kilometers per hour",
1001 "Kilometers per hour",
1053 "meter per second squared",
1054 "meters per second squared",
1057 "Meters per second squared",
1075 "foot per second squared",
1076 "feet per second squared",
1079 "Feet per second squared",
1193 (
M_PI / 180.0) / 60.0,
1204 (
M_PI / 180.0) / 3600.0,
1521#define UNIT_SYSTEM_TOT (((sizeof(bUnitSystems) / B_UNIT_TYPE_TOT) / sizeof(void *)) - 1)
1600 double value_abs = value > 0.0 ? value : -value;
1602 for (
const bUnitDef *unit = unit_start ? unit_start : usys->
units; unit->
name; unit++) {
1610 if (value_abs >= unit->scalar * (0.1 -
EPS)) {
1615 if (value_abs >= unit->scalar * (1.0 -
EPS)) {
1635 const double scaled_value = value / unit->
scalar;
1636 *r_value_a = std::trunc(scaled_value) * unit->
scalar;
1637 *r_value_b = value - (*r_value_a);
1647 const bool variable_width,
1652 bool do_unit_suffix)
1655 if (unit ==
nullptr) {
1665 double value_conv = (value / unit->
scalar) - unit->
bias;
1671 if (variable_width) {
1688 if (variable_width) {
1689 while (i > 0 &&
str[i] ==
'0') {
1693 if (i > 0 &&
str[i] ==
'.') {
1704 if (i < str_maxncpy) {
1706 if (do_unit_suffix) {
1709 while (unit->
name_short[j] && (i < str_maxncpy)) {
1716 if (i >= str_maxncpy) {
1717 i = str_maxncpy - 1;
1755 const bool variable_width,
1761 double value_a, value_b;
1762 unit_dual_convert(value, usys, &unit_a, &unit_b, &value_a, &value_b, main_unit);
1765 if (unit_b > unit_a) {
1767 size_t i =
unit_as_string(
str, str_maxncpy, value_a, prec,
true, usys, unit_a,
'\0',
true);
1770 if (variable_width) {
1777 if (i + 2 < str_maxncpy) {
1782 str + i, str_maxncpy - i, value_b, prec, variable_width, usys, unit_b,
'\0',
true);
1792 return usys !=
nullptr && usys->
units[0].
name !=
nullptr;
1824 int max_offset = usys->
length - 1;
1830 return std::min(units.
length, max_offset);
1832 return std::min(units.
mass, max_offset);
1834 return std::min(units.
time, max_offset);
1869 bool do_unit_suffix,
1873 const bUnitDef *main_unit =
nullptr;
1882 bool variable_width =
true;
1885 variable_width =
false;
1890 str, str_maxncpy, value, prec, variable_width, usys, main_unit);
1916 bool do_unit_suffix)
1926 str, str_maxncpy, value, prec, type,
split,
pad, do_unit_suffix, units);
1936 bool do_unit_suffix)
1941 str, str_maxncpy, value, prec, type, do_split,
pad, do_unit_suffix, units);
1951 bool do_unit_suffix)
1970 switch (unit_type) {
1991 return (ch >= 128 || isalpha(ch));
1996 if (substr ==
nullptr || substr[0] ==
'\0') {
2002 const char *str_found;
2003 if (case_sensitive) {
2004 str_found = strstr(
str, substr);
2012 if (str_found ==
str ||
2018 int len_name = strlen(substr);
2089 const char *str_found = strstr(remaining_str,
"-");
2091 if (str_found ==
nullptr) {
2096 if ((str_found !=
str) &&
ELEM(*(str_found - 1),
'e',
'E')) {
2100 if (*(str_found + 1) ==
' ') {
2104 return str_found + 1;
2116 for (i = 0; i < remaining_str_maxncpy; i++) {
2117 if (remaining_str[i] ==
'\0') {
2118 return remaining_str + i;
2123 if (remaining_str !=
str && i != 0) {
2125 if ((remaining_str[i] ==
'/') &&
ELEM(remaining_str[i - 1],
't',
'T',
'm',
'M') &&
2126 ELEM(remaining_str[i + 1],
's',
'S'))
2132 if (
ELEM(remaining_str[i - 1],
'e',
'E')) {
2137 if (remaining_str[i - 1] ==
' ') {
2142 return remaining_str + i;
2146 return remaining_str + i;
2169 bool changed =
false;
2171 char *remaining_str =
str;
2173 int remaining_str_maxncpy;
2175 remaining_str_maxncpy = str_maxncpy - int(remaining_str -
str);
2176 if (remaining_str_maxncpy <= 2) {
2183 memmove(remaining_str + 1, remaining_str, remaining_str_maxncpy - 2);
2184 *remaining_str =
'(';
2189 remaining_str_maxncpy = str_maxncpy - int(remaining_str -
str);
2190 memmove(remaining_str + 1, remaining_str, remaining_str_maxncpy - 2);
2191 *remaining_str =
')';
2207 for (
int i = start_ofs; i > 0; i--) {
2222 for (i = start_ofs; i < str_maxncpy; i++) {
2223 if (!strchr(
"0123456789eE.",
str[i])) {
2235 const char *replace_str,
2236 bool case_sensitive)
2238 if (str_maxncpy < 0) {
2243 char *str_found =
const_cast<char *
>(
unit_find_str(
str, replace_str, case_sensitive));
2245 if (str_found ==
nullptr) {
2249 int found_ofs = int(str_found -
str);
2258 if (unit->
bias != 0.0) {
2261 if (
len + 1 < str_maxncpy) {
2262 memmove(
str + prev_op_ofs + 1,
str + prev_op_ofs,
len - prev_op_ofs + 1);
2263 str[prev_op_ofs] =
'(';
2271 value_end_ofs = std::min(value_end_ofs, found_ofs);
2273 if (value_end_ofs + len_bias_num < str_maxncpy) {
2274 memmove(
str + value_end_ofs + len_bias_num,
str + value_end_ofs,
len - value_end_ofs + 1);
2275 memcpy(
str + value_end_ofs, str_tmp, len_bias_num);
2276 len += len_bias_num;
2277 found_ofs += len_bias_num;
2278 str_found += len_bias_num;
2282 int len_name = strlen(replace_str);
2283 int len_move = (
len - (found_ofs + len_name)) + 1;
2289 len_num = std::min(len_num, str_maxncpy);
2291 if (found_ofs + len_num + len_move > str_maxncpy) {
2293 len_move -= (found_ofs + len_num + len_move) - str_maxncpy;
2299 memmove(str_found + len_num, str_found + len_name, len_move);
2302 if (found_ofs + len_num > str_maxncpy) {
2304 len_num -= (found_ofs + len_num) - str_maxncpy;
2309 memcpy(str_found, str_tmp, len_num);
2314 str[str_maxncpy - 1] =
'\0';
2315 return found_ofs + len_num;
2319 char *
str,
int str_maxncpy,
char *str_tmp,
double scale_pref,
const bUnitDef *unit)
2324 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->
name_short, case_sensitive);
2326 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->
name_plural,
false);
2328 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->
name_alt, case_sensitive);
2330 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->
name,
false);
2371 const char *str_prev)
2376 if (str_prev && (unit ==
nullptr)) {
2381 if (unit ==
nullptr) {
2409 const double bias = (unit ==
nullptr) ? 0.0 : unit->
bias;
2411 return value * scalar + bias;
2415 char *
str,
int str_maxncpy,
const char *str_prev,
double scale_pref,
int system,
int type)
2422 double scale_pref_base = scale_pref;
2424 bool changed =
false;
2434 scale_pref_base *= default_unit->
scalar;
2438 if (
SNPRINTF(str_tmp,
"(%s)*%.9g",
str, default_unit->
scalar) <
sizeof(str_tmp)) {
2449 while (
unit_replace(
str, str_maxncpy, str_tmp, scale_pref_base, unit)) {
2459 for (
int system_iter = 0; system_iter <
UNIT_SYSTEM_TOT; system_iter++) {
2460 if (system_iter == system) {
2464 if (usys_iter ==
nullptr) {
2471 while ((ofs =
unit_replace(
str + ofs, str_maxncpy - ofs, str_tmp, scale_pref_base, unit))) {
2482 char *str_found =
str;
2484 while ((str_found = strchr(str_found,
SEP_CHR))) {
2485 bool op_found =
false;
2488 for (
const char *ch = str_found + 1; *ch !=
'\0'; ch++) {
2489 if (
ELEM(*ch,
' ',
'\t')) {
2497 *str_found++ = op_found ?
' ' :
'+';
2509 for (
const bUnitDef *unit = usys->
units; unit->
name && (str_maxncpy > 0); unit++) {
2510 if (unit->name_alt ==
nullptr) {
2514 const char *found =
unit_find_str(orig_str, unit->name_short, case_sensitive);
2515 if (found ==
nullptr) {
2519 int offset = int(found - orig_str);
2522 if (offset < str_maxncpy) {
2523 memcpy(
str, orig_str, offset);
2527 offset = str_maxncpy;
2531 orig_str += offset + strlen(unit->name_short);
2532 str_maxncpy -= offset;
2538 str_maxncpy -= len_name;
2549 if (usys ==
nullptr) {
2554 if (unit ==
nullptr) {
2581 if (usys ==
nullptr) {
2591 return (
static_cast<bUnitCollection *
>(
const_cast<void *
>(usys_pt)))->base_unit;
2603 if (unit_index == -1) {
2633 BLI_assert_msg(0,
"identifier for this unit is not specified yet");
2668 if (unit ==
nullptr) {
#define SNPRINTF(dst, format,...)
#define BLI_STR_UTF8_DEGREE_SIGN
#define BLI_STR_UTF8_SUPERSCRIPT_2
#define BLI_STR_UTF8_SUPERSCRIPT_3
Compatibility-like things for windows.
#define USER_UNIT_ADAPTIVE
int pad[32 - sizeof(int)]
#define BLI_assert_msg(a, msg)
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bUnitDef buImperialVelDef[]
static const bUnitCollection * unit_get_system(int system, int type)
double BKE_unit_scalar_get(const void *usys_pt, int index)
static bUnitDef buPowerDef[]
static const bUnitDef * unit_best_fit(double value, const bUnitCollection *usys, const bUnitDef *unit_start, int suppress)
static bUnitDef buMetricLenDef[]
double BKE_unit_value_scale(const UnitSettings &settings, int unit_type, double value)
const char * BKE_unit_identifier_get(const void *usys_pt, int index)
static bUnitDef buFrequencyDef[]
static bool unit_find(const char *str, const bUnitDef *unit)
@ B_UNIT_DEF_CASE_SENSITIVE
double BKE_unit_base_scalar(int system, int type)
static bUnitDef buImperialVolDef[]
static bUnitDef buMetricVelDef[]
static bool is_unit_adaptive(const int type, const PreferredUnits &units)
static bUnitDef buImperialTempDef[]
static int get_preferred_display_unit_index_if_used(int type, const PreferredUnits &units)
static bUnitCollection buMetricTempCollection
static const bUnitCollection buMetricLenCollection
static bUnitDef buImperialAclDef[]
static bUnitCollection buWavelengthLenCollection
static bool ch_is_op_unary(char op)
bool BKE_unit_is_suppressed(const void *usys_pt, int index)
static bUnitDef buMetricVolDef[]
static bUnitDef buMetricMassDef[]
static size_t unit_as_string(char *str, int str_maxncpy, double value, int prec, const bool variable_width, const bUnitCollection *usys, const bUnitDef *unit, char pad, bool do_unit_suffix)
static bUnitCollection buImperialVelCollection
UnitConversion BKE_unit_preferred_conversion_get(const UnitSettings &settings, int unit_type)
MINLINE int max_ii(int a, int b)
static bUnitDef buImperialMassDef[]
int BKE_unit_base_of_type_get(int system, int type)
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
static bUnitDef buImperialAreaDef[]
static int find_previous_non_value_char(const char *str, const int start_ofs)
const char * BKE_unit_display_name_short_get(const void *usys_pt, int index)
static bUnitCollection buImperialAreaCollection
bool BKE_unit_replace_string(char *str, int str_maxncpy, const char *str_prev, double scale_pref, int system, int type)
static int unit_replace(char *str, int str_maxncpy, char *str_tmp, double scale_pref, const bUnitDef *unit)
static bUnitCollection buNaturalRotCollection
static bUnitCollection buNaturalTimeCollection
static const char * unit_find_str(const char *str, const char *substr, bool case_sensitive)
static char * find_next_op(const char *str, char *remaining_str, int remaining_str_maxncpy)
static const bUnitDef * get_preferred_display_unit_if_used(int type, const PreferredUnits &units)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static bUnitCollection buImperialAclCollection
static const bUnitCollection * bUnitSystems[][B_UNIT_TYPE_TOT]
static bUnitCollection buMetricVelCollection
static bUnitCollection buFrequencyCollection
int BKE_preffered_unit_of_type_or_base_get(const UnitSettings &settings, int type)
static bUnitDef buColorTempDef[]
static bUnitCollection buDummyCollection
static bool unit_should_be_split(int type)
static bUnitCollection buMetricVolCollection
static bUnitCollection buMetricAclCollection
static bool is_valid_unit_collection(const bUnitCollection *usys)
static size_t unit_as_string_split_pair(char *str, int str_maxncpy, double value, int prec, const bool variable_width, const bUnitCollection *usys, const bUnitDef *main_unit)
static bool ch_is_op(char op)
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static int find_end_of_value_chars(const char *str, const int str_maxncpy, const int start_ofs)
static bool unit_distribute_negatives(char *str, const int str_maxncpy)
size_t BKE_unit_value_as_string_adaptive(char *str, int str_maxncpy, double value, int prec, int system, int type, bool split, bool pad, bool do_unit_suffix)
void BKE_unit_system_get(int system, int type, const void **r_usys_pt, int *r_len)
size_t BKE_unit_value_as_string(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
static const bUnitDef * unit_detect_from_str(const bUnitCollection *usys, const char *str, const char *str_prev)
bool BKE_unit_string_contains_unit(const char *str, int type)
bool BKE_unit_is_valid(int system, int type)
size_t BKE_unit_value_as_string_scaled(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
static bUnitDef buMetricAclDef[]
double BKE_unit_closest_scalar(double value, int system, int type)
static bUnitCollection buMetricMassCollection
static PreferredUnits preferred_units_from_UnitSettings(const UnitSettings &settings)
static void unit_dual_convert(double value, const bUnitCollection *usys, bUnitDef const **r_unit_a, bUnitDef const **r_unit_b, double *r_value_a, double *r_value_b, const bUnitDef *main_unit)
const char * BKE_unit_name_get(const void *usys_pt, int index)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
static const bUnitDef * unit_default(const bUnitCollection *usys)
static bUnitDef buMetricTempDef[]
int BKE_unit_base_get(const void *usys_pt)
static bUnitDef buNaturalTimeDef[]
static int unit_scale_str(char *str, int str_maxncpy, char *str_tmp, double scale_pref, const bUnitDef *unit, const char *replace_str, bool case_sensitive)
static bUnitDef buNaturalRotDef[]
static bUnitCollection buImperialTempCollection
static bUnitCollection buImperialVolCollection
void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int system, int type)
static bUnitDef buMetricAreaDef[]
static bUnitDef buImperialLenDef[]
MINLINE int integer_digits_d(double d)
const char * BKE_unit_display_name_get(const void *usys_pt, int index)
static bUnitCollection buCameraLenCollection
static char * skip_unary_op(char *str)
static bUnitCollection buImperialMassCollection
BLI_INLINE bool isalpha_or_utf8(const int ch)
static const bUnitDef * unit_find_in_collection(const bUnitCollection *usys, const char *str)
static bUnitDef buWavelengthLenDef[]
bool BKE_unit_is_adaptive(const UnitSettings &settings, int type)
static bUnitCollection buImperialLenCollection
static bUnitDef buCameraLenDef[]
static const char * find_next_negative(const char *str, const char *remaining_str)
static bUnitCollection buPowerCollection
static bUnitCollection buMetricAreaCollection
double BKE_unit_apply_preferred_unit(const UnitSettings &settings, int type, double value)
static bUnitDef buDummyDef[]
static size_t unit_as_string_main(char *str, int str_maxncpy, double value, int prec, int type, bool split, bool pad, bool do_unit_suffix, const PreferredUnits &units)
static bUnitCollection buColorTempCollection
const char * name_display
#define UNIT_COLLECTION_LENGTH(def)