Repository navigation
Expand file tree
/
Copy pathmodel_search.cpp
More file actions
2891 lines (2526 loc) · 88.4 KB
/
Copy pathmodel_search.cpp
File metadata and controls
2891 lines (2526 loc) · 88.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This program is free software; you can redistribute it and / or modify it
// under the terms of the LGPL License either version 2.1 or later.
// License details are available at https://www.gnu.org/licenses/lgpl-2.1.html
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
// Purpose: Search query parsing and matching. Parses user search text into filters and
// matches individual index records against them; query execution over the index lives in
// model_index.cpp.
#include "pch.h"
#include "model_search.h"
#include "model_index.h"
#include "model_items.h"
#include "model_locations.h"
#include "model_tokenizer.h"
df_assert_movable(df::search_t);
df_assert_movable(df::search_term);
// A search result is returned by value for every candidate an ordinary search examines, and one is
// stored per listed item. The related distance sits in padding the type already had, so relations
// cost that path nothing; this fails if a later field pushes the type into another word.
static_assert(sizeof(df::search_result) <= sizeof(void*) * 2 + 8);
constexpr static auto sv_duplicates = "duplicates";
constexpr static auto sv_remote = "remote";
constexpr static auto sv_panorama = "panorama";
void df::search_t::clear_date_properties()
{
std::erase_if(_terms, [](auto&& v) { return v.is_date(); });
_raw.clear();
}
df::search_t df::search_t::parse_path(const std::string_view text)
{
search_t result;
const folder_path folder(text);
if (folder.is_drive() || folder.exists())
{
result.add_selector(item_selector(folder));
}
else
{
const auto last_slash = find_last_slash(text);
const auto has_file_name = last_slash != std::string_view::npos && last_slash + 1 < text.size();
const auto parent = has_file_name ? text.substr(0, last_slash) : std::string_view{};
const auto has_drive_parent = parent.size() == 2 && parent[1] == ':';
if (!has_file_name || (!is_path(parent) && !has_drive_parent))
{
return result;
}
const file_path file(text);
if (file.exists() && file.is_valid())
{
result.add_selector(item_selector(file.folder())).with(file.name());
}
}
return result;
}
// Issue #139: a path that contains spaces is wrapped in double quotes so it reads (and re-parses)
// as a single search term. Only whitespace forces quotes - str::need_quotes also fires on the colon
// in every Windows drive path, and quoting those would change what the address box shows for input
// that never needed it. Already-quoted text is returned unchanged.
std::string df::quote_path_term(const std::string_view path)
{
if (path.find(' ') == std::string_view::npos) return std::string(path);
if (path.front() == '\"' || path.front() == '\'') return std::string(path);
std::string result;
result.reserve(path.size() + 2);
result += '\"';
result.append(path);
result += '\"';
return result;
}
static std::string auto_quote_search_input(const std::string_view text)
{
const auto trimmed = str::trim(text);
if (!df::is_path(trimmed)) return std::string(text);
auto quoted = df::quote_path_term(trimmed);
// Nothing was added, so hand back exactly what was typed rather than the trimmed form (#178).
return quoted.size() == trimmed.size() ? std::string(text) : quoted;
}
df::search_t df::search_t::parse_from_input(const std::string_view text) const
{
// Issue #178: preserve exactly what the user typed (including quotes and terms
// the engine does not specially recognize) so the search bar does not silently
// revert the input to the normalized form. The builder mutators clear _raw, so
// it is stamped here after the search has been fully constructed.
const auto raw = auto_quote_search_input(text);
const auto has_selector = this->has_selector();
if (is_path(text))
{
auto result = parse_path(text);
if (!result.is_empty())
{
result.raw_text(raw);
return result;
}
}
if (has_selector && str::starts(text, "*."))
{
auto a = *this;
const auto s = a.selectors().front();
const item_selector sel(s.folder(), s.is_recursive(), text);
a.clear_selectors().add_selector(sel);
a.raw_text(raw);
return a;
}
auto result = parse(text);
const auto has_query_parts = result.has_selector() &&
(result.selectors().size() > 1 || !result.terms().empty());
result.raw_text(has_query_parts ? std::string(text) : raw);
return result;
}
namespace
{
bool is_distance_token(const std::string_view token)
{
auto i = size_t{0};
while (i < token.size() && (std::isdigit(static_cast<unsigned char>(token[i])) || token[i] == '.')) ++i;
if (i == 0 || i == token.size()) return false;
const auto unit = token.substr(i);
return str::icmp(unit, "km") == 0 || str::icmp(unit, "m") == 0 || str::icmp(unit, "mi") == 0 ||
str::icmp(unit, "mile") == 0 || str::icmp(unit, "miles") == 0;
}
bool is_size_unit_token(const std::string_view token)
{
// Every unit format_size writes, since format_term writes it apart from its number.
return str::icmp(token, "kb") == 0 || str::icmp(token, "mb") == 0 || str::icmp(token, "gb") == 0 ||
str::icmp(token, "tb") == 0;
}
}
std::vector<search_part> df::coalesce_parts(std::vector<search_part> parts)
{
std::vector<search_part> results;
results.reserve(parts.size());
for (auto& part : parts)
{
if (!results.empty() && part.scope.empty() && part.modifier.is_defaults())
{
auto& previous = results.back();
// "size: 1 MB" is the canonical form format_terms emits, so the unit has to survive being
// tokenized away from its number; otherwise re-running an unchanged query reads it as bytes.
if (prop::from_prefix(previous.scope) == prop::file_size && !part.literal &&
is_size_unit_token(part.term) && str::is_probably_num(previous.term))
{
previous.term += part.term;
continue;
}
auto& location = previous;
// absorb only what is recognisably part of the place, so that a following word such as
// "sunset" stays a separate text term; the first unrecognised token ends the location
// an explicitly quoted part is the user asking for a distinct thing, so it is never absorbed
if (find_location_scope(location.scope) && !part.literal &&
(part.after_comma || is_distance_token(part.term) || is_country_code(part.term)))
{
location.term += ", ";
location.term += part.term;
continue;
}
}
results.emplace_back(std::move(part));
}
return results;
}
df::search_t df::search_t::parse(const std::string_view text)
{
const auto trimmed = str::trim(text);
search_t result;
result.raw_text(text);
if (trimmed.size() >= 2 && is_path_sep(trimmed[0]) && is_path_sep(trimmed[1]))
{
const auto share_separator = trimmed.find_first_of("\\/", 2);
if (share_separator == std::string_view::npos || share_separator == 2 || share_separator + 1 == trimmed.size())
{
return result;
}
}
if (is_path(trimmed))
{
result = parse_path(trimmed);
}
if (result.is_empty())
{
if (is_probably_selector(trimmed))
{
result.add_selector(item_selector(trimmed));
}
else
{
search_tokenizer t;
const auto parts = coalesce_parts(t.parse(trimmed));
for (const auto& part : parts)
{
if (part.scope.empty() && item_selector::can_iterate(part.term))
{
result.add_selector(item_selector(part.term));
}
else
{
result.parse_part(part);
}
}
// Existing folder-plus-query parses take precedence. An unavailable absolute path is
// still a scope, so navigation must report it rather than broadening to collection text.
if (!result.has_selector())
{
std::string_view path;
if (is_path(trimmed)) path = trimmed;
else if (parts.size() == 1 && parts.front().scope.empty() && is_path(parts.front().term))
path = parts.front().term;
if (!path.empty())
{
result = {};
result.add_selector(item_selector(path));
}
}
}
}
return result;
}
// Wrapping a value so the tokenizer reads it back whole. The rule lives here alone because every
// formatted term has to agree with the tokenizer's single un-quoting rule.
static std::string quote_term_value(const std::string_view term_text)
{
const char quote_char = term_text.find('\"') == std::string::npos ? '\"' : '\'';
std::string result;
result += quote_char;
// the tokenizer has no escape, so a value containing the delimiter is doubled and un-doubled on read
for (const auto c : term_text)
{
result += c;
if (c == quote_char) result += c;
}
result += quote_char;
return result;
}
static std::string term_quote(const std::string_view term_text)
{
auto has_special_char = std::ranges::any_of(term_text, [](const char c)
{
return c == ' ' || c == '\t' || c == '\'' || c == '"' || c == '!' || c == '-' ||
c == '<' || c == '>' || c == '=' || c == '&' ||
c == '@' || (c != ':' && search_tokenizer::is_delimiter_char(c));
});
// The tokenizer reads a bare "and" or "or" as the operator, so a term that is one of those words
// has to be written quoted to read back as the text it is.
if (!has_special_char && (str::icmp(term_text, "and") == 0 || str::icmp(term_text, "or") == 0 ||
str::icmp(term_text, tt.query_and) == 0 || str::icmp(term_text, tt.query_or) == 0))
{
has_special_char = true;
}
if (!has_special_char)
{
// colons are allowed for folders or duration/time
const auto colon_pos = term_text.find(':');
if (colon_pos != std::string::npos)
{
if (colon_pos != 1) // folder
{
for (auto i = 0u; i < colon_pos; i++)
{
const auto c = term_text[i];
if (c < '0' || c > '9')
{
// if not all digits, like a time ie 12:00
has_special_char = true;
break;
}
}
}
}
}
return has_special_char ? quote_term_value(term_text) : std::string(term_text);
}
// A search term has to read back as itself, so numbers are trimmed rather than rounded away.
static std::string trim_trailing_zeros(std::string v)
{
if (v.find('.') == std::string::npos) return v;
while (!v.empty() && v.back() == '0') v.pop_back();
if (!v.empty() && v.back() == '.') v.pop_back();
return v;
}
static std::string format_search_coordinate(const double v)
{
return trim_trailing_zeros(std::format("{:.6f}", v));
}
static std::string format_search_number(const double v)
{
std::array<char, 64> buffer{};
const auto [end, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), v);
if (ec == std::errc{}) return {buffer.data(), end};
return trim_trailing_zeros(std::format("{:.17g}", v));
}
static std::string format_search_number(const float v)
{
std::array<char, 64> buffer{};
const auto [end, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), v);
if (ec == std::errc{}) return {buffer.data(), end};
return trim_trailing_zeros(std::format("{:.9g}", v));
}
// locations.md 4.2: a radius reads in the same metres and kilometres a user would type.
static std::string format_search_distance(const double km)
{
const auto m = km * 1000.0;
if (km < 1.0) return trim_trailing_zeros(std::format("{:.4f}", m)) + "m";
return trim_trailing_zeros(std::format("{:.4f}", km)) + "km";
}
// locations.md 3.1: quotes are a fallback, not the default spelling. The parser itself decides
// whether the bare form is unambiguous, so this rule can never drift from the grammar.
static std::string quote_location_value(const std::string_view prefix, const std::string_view value,
const df::search_term& term)
{
std::string candidate(prefix);
candidate += value;
const auto reparsed = df::search_t::parse(candidate);
if (reparsed.terms().size() == 1)
{
auto expected = term;
expected.modifiers = df::search_term_modifier{};
if (reparsed.terms().front() == expected) return std::string(value);
}
return term_quote(value);
}
static std::string format_xy(const df::xy16 xy)
{
if (xy.y) return std::format("{}/{}", xy.x, xy.y);
return str::to_string(xy.x);
}
static std::string format_term_value(const df::search_term& term)
{
const auto* const t = term.key;
const auto n = term.int_val;
const auto d = term.float_val;
const auto xy = term.xy_val;
const auto n64 = term.int64_val;
if (t->data_type == prop::data_type::string || !str::is_empty(term.text)) return term.text;
if (t->data_type == prop::data_type::date) return prop::format_date(df::date_t::from_days(n));
if (t == prop::f_number) return prop::format_f_num(d);
if (t == prop::megapixels) return str::print("%1.1f", d);
if (t == prop::dimensions) return prop::format_dimensions({xy.x, xy.y});
if (t == prop::duration) return prop::format_duration(n);
if (t == prop::exposure_time) return format_search_number(static_cast<float>(d)) + "s";
if (t == prop::iso_speed) return prop::format_iso(n);
if (t == prop::latitude) return prop::format_gps(d);
if (t == prop::longitude) return prop::format_gps(d);
if (t == prop::rating) return str::to_string(n);
if (t == prop::audio_sample_rate) return str::to_string(n);
if (t == prop::audio_channels) return prop::format_audio_channels(n);
if (t == prop::audio_sample_type) return format_audio_sample_type(static_cast<prop::audio_sample_t>(n));
if (t == prop::streams) return prop::format_streams(n);
if (t == prop::track_num || t == prop::disk_num || t == prop::episode)
{
return format_xy(xy);
}
if (t == prop::orientation)
{
return str::to_string(n);
}
if (t == prop::year)
{
return str::to_string(n);
}
if (t == prop::focal_length) return format_search_number(static_cast<float>(d)) + "mm";
if (t == prop::file_size) return df::format_size(df::file_size(n64));
if (t->data_type == prop::data_type::int32)
{
return str::to_string(n);
}
if (t->data_type == prop::data_type::uint32)
{
return str::to_string(static_cast<uint32_t>(n));
}
df::assert_true(false);
return {};
}
std::string df::format_term(const search_term& term)
{
std::ostringstream result;
if (term.modifiers.less_than) result << "<";
if (term.modifiers.greater_than) result << ">";
if (term.modifiers.equals) result << "=";
if (term.modifiers.less_than || term.modifiers.greater_than || term.modifiers.equals) result << " ";
if (term.type == search_term_type::has_type)
{
const auto scope = term.modifiers.positive ? tt.query_with : tt.query_without;
result << scope.sv();
result << ':';
result << ' ';
result << term.key->name.sv();
}
else if (term.type == search_term_type::has_location)
{
const auto scope = term.modifiers.positive ? tt.query_with : tt.query_without;
result << scope.sv();
result << ':';
result << ' ';
result << "location";
}
else
{
if (!term.modifiers.positive) result << '-';
if (term.type == search_term_type::text)
{
result << term_quote(term.text);
}
else if (term.type == search_term_type::value)
{
if (prop::tag == term.key)
{
result << '#';
result << term_quote(format_term_value(term));
}
else if (prop::file_size == term.key)
{
// non quoted type
result << term.key->name.sv();
result << ':';
result << ' ';
result << format_term_value(term);
}
else
{
result << term.key->name.sv();
result << ':';
result << ' ';
result << term_quote(format_term_value(term));
}
}
else if (term.type == search_term_type::media_type)
{
result << '@';
result << term.fg_val->display_name(false);
}
else if (term.type == search_term_type::date)
{
if (term.date_val.target == date_parts_prop::created)
{
result << tt.query_created.sv();
result << ":";
}
else if (term.date_val.target == date_parts_prop::original)
{
result << tt.query_original.sv();
result << ":";
}
else if (term.date_val.target == date_parts_prop::modified)
{
result << tt.query_modified.sv();
result << ":";
}
if (term.date_val.age)
{
if (term.date_val.target == date_parts_prop::any)
{
result << tt.query_age.sv();
result << ":";
}
result << str::to_string(term.date_val.age);
}
else
{
const auto is_month_only = term.date_val.year == 0 && term.date_val.day == 0;
bool is_cat = false;
if (term.date_val.year != 0)
{
result << str::to_string(term.date_val.year);
is_cat = true;
}
if (term.date_val.month != 0)
{
if (is_cat) result << "-";
result << (is_month_only
? str::month(term.date_val.month, true)
: str::short_month(term.date_val.month, true));
is_cat = true;
}
if (term.date_val.day != 0)
{
if (is_cat) result << "-";
result << str::to_string(term.date_val.day);
}
}
}
else if (term.type == search_term_type::location)
{
const auto prefix = std::string(location_scope_name(term.level)) + ':';
result << prefix;
if (term.coord_val.is_valid())
{
// A coordinate reads back as the same "lat, lon, radius" a user could type;
// the older `+lat+lon+km` spelling still parses.
std::string value = format_search_coordinate(term.coord_val.latitude());
value += ',';
value += format_search_coordinate(term.coord_val.longitude());
if (term.float_val > 0.0)
{
value += ',';
value += format_search_distance(term.float_val);
}
result << value;
}
else
{
if (term.float_val > 0.0)
{
const auto place_query = std::string(term.text) + ", " + format_search_distance(term.float_val);
result << quote_location_value(prefix, place_query, term);
}
else
{
result << quote_location_value(prefix, term.text, term);
}
}
}
else if (term.type == search_term_type::area)
{
result << "area:" << term_quote(term.text);
}
else if (term.type == search_term_type::extension)
{
result << "ext:";
result << term.text;
}
else if (term.type == search_term_type::volume)
{
result << "volume:";
result << term_quote(term.text);
}
else if (term.type == search_term_type::duplicate)
{
result << "@";
result << sv_duplicates;
}
else if (term.type == search_term_type::remote)
{
result << "@";
result << sv_remote;
}
else if (term.type == search_term_type::panorama)
{
result << "@";
result << sv_panorama;
}
}
return result.str();
}
static void term_join(std::string& result, const df::search_term& term)
{
if (!result.empty()) result += ' ';
if (term.modifiers.logical_op == df::search_term_modifier_bool::m_or)
{
result += tt.query_or;
result += " ";
}
for (auto i = 0; i < term.modifiers.begin_group; i++) result += '(';
result += format_term(term);
for (auto i = 0; i < term.modifiers.end_group; i++) result += ')';
}
static void term_join(std::string& result, const std::string_view term)
{
if (!result.empty()) result += ' ';
result += term_quote(term);
}
static void term_join(std::string& result, const std::string_view scope, const std::string_view term)
{
if (!result.empty()) result += ' ';
result += scope;
result += ':';
result += ' ';
result += term_quote(term);
}
std::string df::search_t::format_terms() const
{
const bool no_filter = _terms.empty() && !has_related();
if (_selectors.size() == 1 && !has_media_type() && no_filter) return _selectors.front().str();
std::string result;
for (const auto& s : _selectors)
{
term_join(result, s.str());
}
if (has_related())
{
term_join(result, tt.query_related, _related.path.str());
}
for (const auto& t : _terms)
{
term_join(result, t);
}
return result;
}
std::string df::search_t::text() const
{
return !str::is_empty(_raw) ? _raw : format_terms();
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
static int days_per_month(const int month, const int year = -1)
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
if (month == 2)
{
const auto leap_year = (year >= 0 && year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
return leap_year ? 29 : 28;
}
return 31;
}
df::date_parts df::search_t::find_date_parts() const
{
date_parts result;
for (const auto& t : _terms)
{
if (t.type == search_term_type::date && t.no_modifiers())
{
if (t.date_val.age) result.age = t.date_val.age;
if (t.date_val.day) result.day = t.date_val.day;
if (t.date_val.month) result.month = t.date_val.month;
if (t.date_val.year) result.year = t.date_val.year;
result.target = t.date_val.target;
}
}
return result;
}
void df::search_t::next_date(const bool forward)
{
auto parts = find_date_parts();
if (parts.day && parts.month)
{
if (forward)
{
// The year decides February's length, and passing only the month left it at 28 - so
// stepping forward from the 28th of a leap February skipped the 29th entirely. A
// year-less date still takes the 28-day reading, which is what it did before.
const auto days_in_month = days_per_month(parts.month, parts.year ? parts.year : -1);
parts.day += 1;
if (parts.day > days_in_month)
{
parts.day = 1;
parts.month += 1;
if (parts.month > 12)
{
parts.month = 1;
if (parts.year)
{
parts.year += 1;
}
}
}
}
else
{
parts.day -= 1;
if (parts.day < 1)
{
parts.month -= 1;
if (parts.month < 1)
{
parts.month = 12;
if (parts.year)
{
parts.year -= 1;
}
}
parts.day = days_per_month(parts.month, parts.year ? parts.year : -1);
}
}
clear_date_properties();
day(parts.day, parts.month, parts.year, parts.target);
}
else if (parts.year && parts.month)
{
if (forward)
{
parts.month += 1;
if (parts.month > 12)
{
parts.year += 1;
parts.month = 1;
}
}
else
{
parts.month -= 1;
if (parts.month < 1)
{
parts.year -= 1;
parts.month = 12;
}
}
clear_date_properties();
day(0, parts.month, parts.year, parts.target);
}
else if (parts.year)
{
parts.year += forward ? 1 : -1;
clear_date_properties();
year(parts.year, parts.target);
}
else if (parts.month)
{
if (forward)
{
parts.month += 1;
if (parts.month > 12)
{
parts.month = 1;
}
}
else
{
parts.month -= 1;
if (parts.month < 1)
{
parts.month = 12;
}
}
clear_date_properties();
month(parts.month, parts.target);
}
}
search_presence_mask df::search_t::calc_required_presence() const
{
search_presence_mask result;
// Presence masks are a rejection-only optimisation: every exact match must contain every
// bit returned here. A negated term requires absence rather than presence, and an OR
// only requires one branch, so neither can safely contribute required bits. Disable
// the prefilter for the whole expression when OR is present; deriving common bits
// across nested groups would require evaluating the Boolean expression tree.
if (std::ranges::any_of(_terms, [](const search_term& term)
{
return term.modifiers.logical_op == search_term_modifier_bool::m_or;
}))
{
return result;
}
for (const auto& v : _terms)
{
if (!v.modifiers.positive) continue;
switch (v.type)
{
case search_term_type::duplicate:
result.types |= search_presence_mask::duplicates;
break;
case search_term_type::remote:
// Only a coordinate can be remote, so the prefilter may skip everything else.
result.types |= search_presence_mask::location;
break;
case search_term_type::panorama:
result.types |= search_presence_mask::panorama;
break;
case search_term_type::media_type:
result.types |= v.fg_val->search_presence_bit();
break;
case search_term_type::value: break;
case search_term_type::has_type: break;
case search_term_type::date:
result.types |= v.key->search_presence_bit;
break;
case search_term_type::empty: break;
case search_term_type::text: break;
default:
break;
}
}
return result;
}
df::date_parts month_and_day(const std::string_view s)
{
df::date_parts result;
const auto parts = str::split(
s, true, [](const char c) { return c == '-' || c == '/' || c == '\\' || c == '.'; });
if (parts.size() == 2)
{
const auto month = str::month(parts[0]);
if (month != 0 && str::is_num(parts[1]))
{
const auto d = str::to_int(parts[1]);
if (d >= 1 && d <= 31)
{
result.month = month;
result.day = d;
}
}
}
return result;
}
df::date_parts year_and_month(const std::string_view s)
{
df::date_parts result;
const auto parts = str::split(
s, true, [](const char c) { return c == '-' || c == '/' || c == '\\' || c == '.'; });
if (parts.size() == 2)
{
const auto month = str::month(parts[1]);
if (month != 0 && str::is_num(parts[0]))
{
const auto y = str::to_int(parts[0]);
if (y > 1900 && y < 2999)
{
result.month = month;
result.year = y;
}
}
}
return result;
}
namespace
{
struct place_query
{
std::string name;
double km = 0.0;
};
// locations.md 3.1: a trailing <number><unit> component is a radius, with or without a comma.
// Recognised only when it parses completely, so a place named like a distance still resolves.
place_query split_place_query(const std::string_view text)
{
place_query result;
result.name.assign(str::trim(text));
if (result.name.empty()) return result;
const auto tail = std::string_view(result.name);
auto unit_start = tail.size();
while (unit_start > 0 && std::isalpha(static_cast<unsigned char>(tail[unit_start - 1]))) --unit_start;
if (unit_start == tail.size()) return result;
const auto unit = tail.substr(unit_start);
double scale = 0.0;
if (str::icmp(unit, "km") == 0) scale = 1.0;
else if (str::icmp(unit, "m") == 0) scale = 0.001;
else if (str::icmp(unit, "mi") == 0 || str::icmp(unit, "mile") == 0 || str::icmp(unit, "miles") == 0)
scale = 1.609344;
else return result;
auto number_end = unit_start;
while (number_end > 0 && std::isspace(static_cast<unsigned char>(tail[number_end - 1]))) --number_end;
auto number_start = number_end;
while (number_start > 0 &&
(std::isdigit(static_cast<unsigned char>(tail[number_start - 1])) || tail[number_start - 1] == '.'))
{
--number_start;
}
if (number_start == number_end || number_start == 0) return result;
const auto separator = tail[number_start - 1];
if (separator != ',' && !std::isspace(static_cast<unsigned char>(separator))) return result;
const auto value = str::to_double(tail.substr(number_start, number_end - number_start));
if (value <= 0.0) return result;
result.km = value * scale;
result.name.erase(number_start);
while (!result.name.empty() && std::isspace(static_cast<unsigned char>(result.name.back()))) result.name.
pop_back();
if (!result.name.empty() && result.name.back() == ',') result.name.pop_back();
while (!result.name.empty() && std::isspace(static_cast<unsigned char>(result.name.back()))) result.name.
pop_back();
return result;
}
// locations.md 3.1: "lat, lon" is a coordinate, not a place name. Both parts must be
// numbers in range, so a place whose name contains digits still resolves by name.
bool split_coordinate(const std::string_view text, gps_coordinate& coord)
{
const auto comma = text.find(',');