/usr/include/xapian/queryparser.h is in libxapian-dev 1.4.5-1ubuntu0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 | /** @file queryparser.h
* @brief parsing a user query string to build a Xapian::Query object
*/
/* Copyright (C) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017 Olly Betts
* Copyright (C) 2010 Adam Sjøgren
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifndef XAPIAN_INCLUDED_QUERYPARSER_H
#define XAPIAN_INCLUDED_QUERYPARSER_H
#if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
# error "Never use <xapian/queryparser.h> directly; include <xapian.h> instead."
#endif
#include <xapian/attributes.h>
#include <xapian/deprecated.h>
#include <xapian/intrusive_ptr.h>
#include <xapian/query.h>
#include <xapian/termiterator.h>
#include <xapian/visibility.h>
#include <set>
#include <string>
namespace Xapian {
class Database;
class Stem;
/// Base class for stop-word decision functor.
class XAPIAN_VISIBILITY_DEFAULT Stopper
: public Xapian::Internal::opt_intrusive_base {
/// Don't allow assignment.
void operator=(const Stopper &);
/// Don't allow copying.
Stopper(const Stopper &);
public:
/// Default constructor.
Stopper() { }
/** Is term a stop-word?
*
* @param term The term to test.
*/
virtual bool operator()(const std::string & term) const = 0;
/// Class has virtual methods, so provide a virtual destructor.
virtual ~Stopper() { }
/// Return a string describing this object.
virtual std::string get_description() const;
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated Stopper
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
Stopper * release() {
opt_intrusive_base::release();
return this;
}
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated Stopper
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
const Stopper * release() const {
opt_intrusive_base::release();
return this;
}
};
/// Simple implementation of Stopper class - this will suit most users.
class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
std::set<std::string> stop_words;
public:
/// Default constructor.
SimpleStopper() { }
/** Initialise from a pair of iterators.
*
* Xapian includes stop list files for many languages. You can initialise from a file like that:
* @code
* ifstream words("stopwords/english/stop.txt");
* Xapian::SimplerStopper stopper(istream_iterator<string>(words), istream_iterator<string>());
* @endcode
*
*/
template <class Iterator>
SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
/// Add a single stop word.
void add(const std::string & word) { stop_words.insert(word); }
virtual bool operator()(const std::string & term) const {
return stop_words.find(term) != stop_words.end();
}
virtual std::string get_description() const;
};
enum {
RP_SUFFIX = 1,
RP_REPEATED = 2,
RP_DATE_PREFER_MDY = 4
};
/// Base class for range processors.
class XAPIAN_VISIBILITY_DEFAULT RangeProcessor
: public Xapian::Internal::opt_intrusive_base {
/// Don't allow assignment.
void operator=(const RangeProcessor &);
/// Don't allow copying.
RangeProcessor(const RangeProcessor &);
protected:
/** The value slot to process.
*
* If this range processor isn't value-based, it can ignore this member.
*/
Xapian::valueno slot;
/** The prefix (or suffix with RP_SUFFIX) string to look for. */
std::string str;
/** Flags.
*
* Bitwise-or (| in C++) of zero or more of the following:
* * Xapian::RP_SUFFIX - require @a str as a suffix
* instead of a prefix.
* * Xapian::RP_REPEATED - optionally allow @a str
* on both ends of the range - e.g. $1..$10 or
* 5m..50m. By default a prefix is only checked for on
* the start (e.g. date:1/1/1980..31/12/1989), and a
* suffix only on the end (e.g. 2..12kg).
*/
unsigned flags;
public:
/** Default constructor. */
RangeProcessor() : slot(Xapian::BAD_VALUENO), flags(0) { }
/** Constructor.
*
* @param slot_ Which value slot to generate ranges over.
* @param str_ A string to look for to recognise values as belonging
* to this range (as a prefix by default, or as a suffix
* if flags Xapian::RP_SUFFIX is specified).
* @param flags_ Zero or more of the following flags, combined with
* bitwise-or (| in C++):
* * Xapian::RP_SUFFIX - require @a str_ as a suffix
* instead of a prefix.
* * Xapian::RP_REPEATED - optionally allow @a str_
* on both ends of the range - e.g. $1..$10 or
* 5m..50m. By default a prefix is only checked for on
* the start (e.g. date:1/1/1980..31/12/1989), and a
* suffix only on the end (e.g. 2..12kg).
*/
explicit RangeProcessor(Xapian::valueno slot_,
const std::string& str_ = std::string(),
unsigned flags_ = 0)
: slot(slot_), str(str_), flags(flags_) { }
/// Destructor.
virtual ~RangeProcessor();
/** Check prefix/suffix on range.
*
* If they match, remove the prefix/suffix and then call operator()()
* to try to handle the range.
*/
Xapian::Query check_range(const std::string& b, const std::string& e);
/** Check for a valid range of this type.
*
* Override this method to implement your own range handling.
*
* @param begin The start of the range as specified in the query string
* by the user.
* @param end The end of the range as specified in the query string
* by the user (empty string for no upper limit).
*
* @return An OP_VALUE_RANGE Query object (or if end.empty(), an
* OP_VALUE_GE Query object).
*/
virtual Xapian::Query
operator()(const std::string &begin, const std::string &end);
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated RangeProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
RangeProcessor * release() {
opt_intrusive_base::release();
return this;
}
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated RangeProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
const RangeProcessor * release() const {
opt_intrusive_base::release();
return this;
}
};
/** Handle a date range.
*
* Begin and end must be dates in a recognised format.
*/
class XAPIAN_VISIBILITY_DEFAULT DateRangeProcessor : public RangeProcessor {
int epoch_year;
public:
/** Constructor.
*
* @param slot_ The value number to return from operator().
*
* @param flags_ Zero or more of the following flags, combined with
* bitwise-or:
* * Xapian::RP_DATE_PREFER_MDY - interpret ambiguous
* dates as month/day/year rather than day/month/year.
*
* @param epoch_year_ Year to use as the epoch for dates with 2 digit
* years (default: 1970, so 1/1/69 is 2069 while
* 1/1/70 is 1970).
*/
explicit DateRangeProcessor(Xapian::valueno slot_,
unsigned flags_ = 0,
int epoch_year_ = 1970)
: RangeProcessor(slot_, std::string(), flags_),
epoch_year(epoch_year_) { }
/** Constructor.
*
* @param slot_ The value slot number to query.
*
* @param str_ A string to look for to recognise values as belonging
* to this date range.
*
* @param flags_ Zero or more of the following flags, combined with
* bitwise-or:
* * Xapian::RP_SUFFIX - require @a str_ as a suffix
* instead of a prefix.
* * Xapian::RP_REPEATED - optionally allow @a str_
* on both ends of the range - e.g. $1..$10 or
* 5m..50m. By default a prefix is only checked for on
* the start (e.g. date:1/1/1980..31/12/1989), and a
* suffix only on the end (e.g. 2..12kg).
* * Xapian::RP_DATE_PREFER_MDY - interpret ambiguous
* dates as month/day/year rather than day/month/year.
*
* @param epoch_year_ Year to use as the epoch for dates with 2 digit
* years (default: 1970, so 1/1/69 is 2069 while
* 1/1/70 is 1970).
*
* The string supplied in str_ is used by @a operator() to decide whether
* the pair of strings supplied to it constitute a valid range. If
* prefix_ is true, the first value in a range must begin with str_ (and
* the second value may optionally begin with str_);
* if prefix_ is false, the second value in a range must end with str_
* (and the first value may optionally end with str_).
*
* If str_ is empty, the Xapian::RP_SUFFIX and Xapian::RP_REPEATED are
* irrelevant, and no special strings are required at the start or end of
* the strings defining the range.
*
* The remainder of both strings defining the endpoints must be valid
* dates.
*
* For example, if str_ is "created:", Xapian::RP_SUFFIX is not specified,
* and the range processor has been added to the queryparser, the
* queryparser will accept "created:1/1/2000..31/12/2001".
*/
DateRangeProcessor(Xapian::valueno slot_, const std::string &str_,
unsigned flags_ = 0, int epoch_year_ = 1970)
: RangeProcessor(slot_, str_, flags_),
epoch_year(epoch_year_) { }
/** Check for a valid date range.
*
* If any specified prefix is present, and the range looks like a
* date range, the dates are converted to the format YYYYMMDD and
* combined into a value range query.
*
* @param begin The start of the range as specified in the query string
* by the user.
* @param end The end of the range as specified in the query string
* by the user.
*/
Xapian::Query operator()(const std::string& begin, const std::string& end);
};
/** Handle a number range.
*
* This class must be used on values which have been encoded using
* Xapian::sortable_serialise() which turns numbers into strings which
* will sort in the same order as the numbers (the same values can be
* used to implement a numeric sort).
*/
class XAPIAN_VISIBILITY_DEFAULT NumberRangeProcessor : public RangeProcessor {
public:
/** Constructor.
*
* @param slot_ The value slot number to query.
*
* @param str_ A string to look for to recognise values as belonging
* to this numeric range.
*
* @param flags_ Zero or more of the following flags, combined with
* bitwise-or:
* * Xapian::RP_SUFFIX - require @a str_ as a suffix
* instead of a prefix.
* * Xapian::RP_REPEATED - optionally allow @a str_
* on both ends of the range - e.g. $1..$10 or
* 5m..50m. By default a prefix is only checked for on
* the start (e.g. date:1/1/1980..31/12/1989), and a
* suffix only on the end (e.g. 2..12kg).
*
* The string supplied in str_ is used by @a operator() to decide whether
* the pair of strings supplied to it constitute a valid range. If
* prefix_ is true, the first value in a range must begin with str_ (and
* the second value may optionally begin with str_);
* if prefix_ is false, the second value in a range must end with str_
* (and the first value may optionally end with str_).
*
* If str_ is empty, the setting of prefix_ is irrelevant, and no special
* strings are required at the start or end of the strings defining the
* range.
*
* The remainder of both strings defining the endpoints must be valid
* floating point numbers. (FIXME: define format recognised).
*
* For example, if str_ is "$" and prefix_ is true, and the range
* processor has been added to the queryparser, the queryparser will
* accept "$10..50" or "$10..$50", but not "10..50" or "10..$50" as valid
* ranges. If str_ is "kg" and prefix_ is false, the queryparser will
* accept "10..50kg" or "10kg..50kg", but not "10..50" or "10kg..50" as
* valid ranges.
*/
NumberRangeProcessor(Xapian::valueno slot_,
const std::string &str_ = std::string(),
unsigned flags_ = 0)
: RangeProcessor(slot_, str_, flags_) { }
/** Check for a valid numeric range.
*
* If BEGIN..END is a valid numeric range with the specified prefix/suffix
* (if one was specified), the prefix/suffix is removed, the string
* converted to a number, and encoded with Xapian::sortable_serialise(),
* and a value range query is built.
*
* @param begin The start of the range as specified in the query string
* by the user.
* @param end The end of the range as specified in the query string
* by the user.
*/
Xapian::Query operator()(const std::string& begin, const std::string& end);
};
/// Base class for value range processors.
class XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor
: public Xapian::Internal::opt_intrusive_base {
/// Don't allow assignment.
void operator=(const ValueRangeProcessor &);
/// Don't allow copying.
ValueRangeProcessor(const ValueRangeProcessor &);
public:
/// Default constructor.
ValueRangeProcessor() { }
/// Destructor.
virtual ~ValueRangeProcessor();
/** Check for a valid range of this type.
*
* @param[in,out] begin The start of the range as specified in the query
* string by the user. This parameter is a
* non-const reference so the ValueRangeProcessor
* can modify it to return the value to start the
* range with.
* @param[in,out] end The end of the range. This is also a non-const
* reference so it can be modified.
*
* @return If this ValueRangeProcessor recognises the range BEGIN..END it
* returns the value slot number to range filter on. Otherwise it
* returns Xapian::BAD_VALUENO.
*/
virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated ValueRangeProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
ValueRangeProcessor * release() {
opt_intrusive_base::release();
return this;
}
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated ValueRangeProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
const ValueRangeProcessor * release() const {
opt_intrusive_base::release();
return this;
}
};
/** Handle a string range.
*
* The end points can be any strings.
*
* @deprecated Use Xapian::RangeProcessor instead (added in 1.3.6).
*/
class XAPIAN_DEPRECATED_CLASS_EX XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
protected:
/** The value slot to process. */
Xapian::valueno valno;
/** Whether to look for @a str as a prefix or suffix. */
bool prefix;
/** The prefix (or suffix if prefix==false) string to look for. */
std::string str;
public:
/** Constructor.
*
* @param slot_ The value number to return from operator().
*/
explicit StringValueRangeProcessor(Xapian::valueno slot_)
: valno(slot_), str() { }
/** Constructor.
*
* @param slot_ The value number to return from operator().
* @param str_ A string to look for to recognise values as belonging
* to this range.
* @param prefix_ Flag specifying whether to check for str_ as a prefix
* or a suffix.
*/
StringValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
bool prefix_ = true)
: valno(slot_), prefix(prefix_), str(str_) { }
/** Check for a valid string range.
*
* @param[in,out] begin The start of the range as specified in the
* query string by the user. This parameter is a
* non-const reference so the ValueRangeProcessor
* can modify it to return the value to start the
* range with.
* @param[in,out] end The end of the range. This is also a non-const
* reference so it can be modified.
*
* @return A StringValueRangeProcessor always accepts a range it is
* offered, and returns the value of slot_ passed at construction
* time. It doesn't modify @a begin or @a end.
*/
Xapian::valueno operator()(std::string &begin, std::string &end);
};
/** Handle a date range.
*
* Begin and end must be dates in a recognised format.
*
* @deprecated Use Xapian::DateRangeProcessor instead (added in 1.3.6).
*/
class XAPIAN_DEPRECATED_CLASS_EX XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public StringValueRangeProcessor {
bool prefer_mdy;
int epoch_year;
public:
/** Constructor.
*
* @param slot_ The value number to return from operator().
* @param prefer_mdy_ Should ambiguous dates be interpreted as
* month/day/year rather than day/month/year?
* (default: false)
* @param epoch_year_ Year to use as the epoch for dates with 2 digit
* years (default: 1970, so 1/1/69 is 2069 while
* 1/1/70 is 1970).
*/
DateValueRangeProcessor(Xapian::valueno slot_, bool prefer_mdy_ = false,
int epoch_year_ = 1970)
: StringValueRangeProcessor(slot_),
prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
/** Constructor.
*
* @param slot_ The value number to return from operator().
*
* @param str_ A string to look for to recognise values as belonging
* to this date range.
*
* @param prefix_ Whether to look for the string at the start or end of
* the values. If true, the string is a prefix; if
* false, the string is a suffix (default: true).
*
* @param prefer_mdy_ Should ambiguous dates be interpreted as
* month/day/year rather than day/month/year?
* (default: false)
*
* @param epoch_year_ Year to use as the epoch for dates with 2 digit
* years (default: 1970, so 1/1/69 is 2069 while
* 1/1/70 is 1970).
*
* The string supplied in str_ is used by @a operator() to decide whether
* the pair of strings supplied to it constitute a valid range. If
* prefix_ is true, the first value in a range must begin with str_ (and
* the second value may optionally begin with str_);
* if prefix_ is false, the second value in a range must end with str_
* (and the first value may optionally end with str_).
*
* If str_ is empty, the setting of prefix_ is irrelevant, and no special
* strings are required at the start or end of the strings defining the
* range.
*
* The remainder of both strings defining the endpoints must be valid
* dates.
*
* For example, if str_ is "created:" and prefix_ is true, and the range
* processor has been added to the queryparser, the queryparser will
* accept "created:1/1/2000..31/12/2001".
*/
DateValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
bool prefix_ = true,
bool prefer_mdy_ = false, int epoch_year_ = 1970)
: StringValueRangeProcessor(slot_, str_, prefix_),
prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
#ifndef SWIG
/** Constructor.
*
* This is like the previous version, but with const char * instead of
* std::string - we need this overload as otherwise
* DateValueRangeProcessor(1, "date:") quietly interprets the second
* argument as a boolean in preference to std::string. If you want to
* be compatible with 1.2.12 and earlier, then explicitly convert to
* std::string, i.e.: DateValueRangeProcessor(1, std::string("date:"))
*
* @param slot_ The value number to return from operator().
*
* @param str_ A string to look for to recognise values as belonging
* to this date range.
*
* @param prefix_ Whether to look for the string at the start or end of
* the values. If true, the string is a prefix; if
* false, the string is a suffix (default: true).
*
* @param prefer_mdy_ Should ambiguous dates be interpreted as
* month/day/year rather than day/month/year?
* (default: false)
*
* @param epoch_year_ Year to use as the epoch for dates with 2 digit
* years (default: 1970, so 1/1/69 is 2069 while
* 1/1/70 is 1970).
*
* The string supplied in str_ is used by @a operator() to decide whether
* the pair of strings supplied to it constitute a valid range. If
* prefix_ is true, the first value in a range must begin with str_ (and
* the second value may optionally begin with str_);
* if prefix_ is false, the second value in a range must end with str_
* (and the first value may optionally end with str_).
*
* If str_ is empty, the setting of prefix_ is irrelevant, and no special
* strings are required at the start or end of the strings defining the
* range.
*
* The remainder of both strings defining the endpoints must be valid
* dates.
*
* For example, if str_ is "created:" and prefix_ is true, and the range
* processor has been added to the queryparser, the queryparser will
* accept "created:1/1/2000..31/12/2001".
*/
DateValueRangeProcessor(Xapian::valueno slot_, const char * str_,
bool prefix_ = true,
bool prefer_mdy_ = false, int epoch_year_ = 1970)
: StringValueRangeProcessor(slot_, str_, prefix_),
prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
#endif
/** Check for a valid date range.
*
* @param[in,out] begin The start of the range as specified in the
* query string by the user. This parameter is a
* non-const reference so the ValueRangeProcessor
* can modify it to return the value to start the
* range with.
* @param[in,out] end The end of the range. This is also a non-const
* reference so it can be modified.
*
* @return If BEGIN..END is a sensible date range, this method modifies
* them into the format YYYYMMDD and returns the value of slot_
* passed at construction time. Otherwise it returns
* Xapian::BAD_VALUENO.
*/
Xapian::valueno operator()(std::string &begin, std::string &end);
};
/** Handle a number range.
*
* This class must be used on values which have been encoded using
* Xapian::sortable_serialise() which turns numbers into strings which
* will sort in the same order as the numbers (the same values can be
* used to implement a numeric sort).
*
* @deprecated Use Xapian::NumberRangeProcessor instead (added in 1.3.6).
*/
class XAPIAN_DEPRECATED_CLASS_EX XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public StringValueRangeProcessor {
public:
/** Constructor.
*
* @param slot_ The value number to return from operator().
*/
explicit NumberValueRangeProcessor(Xapian::valueno slot_)
: StringValueRangeProcessor(slot_) { }
/** Constructor.
*
* @param slot_ The value number to return from operator().
*
* @param str_ A string to look for to recognise values as belonging
* to this numeric range.
*
* @param prefix_ Whether to look for the string at the start or end of
* the values. If true, the string is a prefix; if
* false, the string is a suffix (default: true).
*
* The string supplied in str_ is used by @a operator() to decide whether
* the pair of strings supplied to it constitute a valid range. If
* prefix_ is true, the first value in a range must begin with str_ (and
* the second value may optionally begin with str_);
* if prefix_ is false, the second value in a range must end with str_
* (and the first value may optionally end with str_).
*
* If str_ is empty, the setting of prefix_ is irrelevant, and no special
* strings are required at the start or end of the strings defining the
* range.
*
* The remainder of both strings defining the endpoints must be valid
* floating point numbers. (FIXME: define format recognised).
*
* For example, if str_ is "$" and prefix_ is true, and the range
* processor has been added to the queryparser, the queryparser will
* accept "$10..50" or "$10..$50", but not "10..50" or "10..$50" as valid
* ranges. If str_ is "kg" and prefix_ is false, the queryparser will
* accept "10..50kg" or "10kg..50kg", but not "10..50" or "10kg..50" as
* valid ranges.
*/
NumberValueRangeProcessor(Xapian::valueno slot_, const std::string &str_,
bool prefix_ = true)
: StringValueRangeProcessor(slot_, str_, prefix_) { }
/** Check for a valid numeric range.
*
* @param[in,out] begin The start of the range as specified in the
* query string by the user. This parameter is a
* non-const reference so the ValueRangeProcessor
* can modify it to return the value to start the
* range with.
* @param[in,out] end The end of the range. This is also a non-const
* reference so it can be modified.
*
* @return If BEGIN..END is a valid numeric range with the specified
* prefix/suffix (if one was specified), this method modifies
* them by removing the prefix/suffix, converting to a number,
* and encoding with Xapian::sortable_serialise(), and returns the
* value of slot_ passed at construction time. Otherwise it
* returns Xapian::BAD_VALUENO.
*/
Xapian::valueno operator()(std::string &begin, std::string &end);
};
/** Base class for field processors.
*/
class XAPIAN_VISIBILITY_DEFAULT FieldProcessor
: public Xapian::Internal::opt_intrusive_base {
/// Don't allow assignment.
void operator=(const FieldProcessor &);
/// Don't allow copying.
FieldProcessor(const FieldProcessor &);
public:
/// Default constructor.
FieldProcessor() { }
/// Destructor.
virtual ~FieldProcessor();
/** Convert a field-prefixed string to a Query object.
*
* @param str The string to convert.
*
* @return Query object corresponding to @a str.
*/
virtual Xapian::Query operator()(const std::string &str) = 0;
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated FieldProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
FieldProcessor * release() {
opt_intrusive_base::release();
return this;
}
/** Start reference counting this object.
*
* You can hand ownership of a dynamically allocated FieldProcessor
* object to Xapian by calling release() and then passing the object to a
* Xapian method. Xapian will arrange to delete the object once it is no
* longer required.
*/
const FieldProcessor * release() const {
opt_intrusive_base::release();
return this;
}
};
/// Build a Xapian::Query object from a user query string.
class XAPIAN_VISIBILITY_DEFAULT QueryParser {
public:
/// Class representing the queryparser internals.
class Internal;
/// @private @internal Reference counted internals.
Xapian::Internal::intrusive_ptr<Internal> internal;
/// Enum of feature flags.
typedef enum {
/// Support AND, OR, etc and bracketed subexpressions.
FLAG_BOOLEAN = 1,
/// Support quoted phrases.
FLAG_PHRASE = 2,
/// Support + and -.
FLAG_LOVEHATE = 4,
/// Support AND, OR, etc even if they aren't in ALLCAPS.
FLAG_BOOLEAN_ANY_CASE = 8,
/** Support wildcards.
*
* At present only right truncation (e.g. Xap*) is supported.
*
* Currently you can't use wildcards with boolean filter prefixes,
* or in a phrase (either an explicitly quoted one, or one implicitly
* generated by hyphens or other punctuation).
*
* In Xapian 1.2.x, you needed to tell the QueryParser object which
* database to expand wildcards from by calling set_database(). In
* Xapian 1.3.3, OP_WILDCARD was added and wildcards are now
* expanded when Enquire::get_mset() is called, with the expansion
* using the database being searched.
*/
FLAG_WILDCARD = 16,
/** Allow queries such as 'NOT apples'.
*
* These require the use of a list of all documents in the database
* which is potentially expensive, so this feature isn't enabled by
* default.
*/
FLAG_PURE_NOT = 32,
/** Enable partial matching.
*
* Partial matching causes the parser to treat the query as a
* "partially entered" search. This will automatically treat the
* final word as a wildcarded match, unless it is followed by
* whitespace, to produce more stable results from interactive
* searches.
*
* Currently FLAG_PARTIAL doesn't do anything if the final word
* in the query has a boolean filter prefix, or if it is in a phrase
* (either an explicitly quoted one, or one implicitly generated by
* hyphens or other punctuation). It also doesn't do anything if
* if the final word is part of a value range.
*
* In Xapian 1.2.x, you needed to tell the QueryParser object which
* database to expand wildcards from by calling set_database(). In
* Xapian 1.3.3, OP_WILDCARD was added and wildcards are now
* expanded when Enquire::get_mset() is called, with the expansion
* using the database being searched.
*/
FLAG_PARTIAL = 64,
/** Enable spelling correction.
*
* For each word in the query which doesn't exist as a term in the
* database, Database::get_spelling_suggestion() will be called and if
* a suggestion is returned, a corrected version of the query string
* will be built up which can be read using
* QueryParser::get_corrected_query_string(). The query returned is
* based on the uncorrected query string however - if you want a
* parsed query based on the corrected query string, you must call
* QueryParser::parse_query() again.
*
* NB: You must also call set_database() for this to work.
*/
FLAG_SPELLING_CORRECTION = 128,
/** Enable synonym operator '~'.
*
* NB: You must also call set_database() for this to work.
*/
FLAG_SYNONYM = 256,
/** Enable automatic use of synonyms for single terms.
*
* NB: You must also call set_database() for this to work.
*/
FLAG_AUTO_SYNONYMS = 512,
/** Enable automatic use of synonyms for single terms and groups of
* terms.
*
* NB: You must also call set_database() for this to work.
*/
FLAG_AUTO_MULTIWORD_SYNONYMS = 1024,
/** Enable generation of n-grams from CJK text.
*
* With this enabled, spans of CJK characters are split into unigrams
* and bigrams, with the unigrams carrying positional information.
* Non-CJK characters are split into words as normal.
*
* The corresponding option needs to have been used at index time.
*
* Flag added in Xapian 1.3.4 and 1.2.22, but this mode can be
* enabled in 1.2.8 and later by setting environment variable
* XAPIAN_CJK_NGRAM.
*/
FLAG_CJK_NGRAM = 2048,
/** The default flags.
*
* Used if you don't explicitly pass any to @a parse_query().
* The default flags are FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE.
*
* Added in Xapian 1.0.11.
*/
FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
} feature_flag;
/// Stemming strategies, for use with set_stemming_strategy().
typedef enum { STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z } stem_strategy;
/// Copy constructor.
QueryParser(const QueryParser & o);
/// Assignment.
QueryParser & operator=(const QueryParser & o);
/// Default constructor.
QueryParser();
/// Destructor.
~QueryParser();
/** Set the stemmer.
*
* This sets the stemming algorithm which will be used by the query
* parser. The stemming algorithm will be used according to the stemming
* strategy set by set_stemming_strategy(). As of 1.3.1, this defaults
* to STEM_SOME, but in earlier versions the default was STEM_NONE. If
* you want to work with older versions, you should explicitly set
* a stemming strategy as well as setting a stemmer, otherwise your
* stemmer won't actually be used.
*
* @param stemmer The Xapian::Stem object to set.
*/
void set_stemmer(const Xapian::Stem & stemmer);
/** Set the stemming strategy.
*
* This controls how the query parser will apply the stemming algorithm.
* Note that the stemming algorithm is only applied to words in
* probabilistic fields - boolean filter terms are never stemmed.
*
* @param strategy The strategy to use - possible values are:
* - STEM_NONE: Don't perform any stemming. (default in Xapian <=
* 1.3.0)
* - STEM_SOME: Stem all terms except for those which start with a
* capital letter, or are followed by certain characters
* (currently: <code>(/\@<>=*[{"</code> ), or are used
* with operators which need positional information.
* Stemmed terms are prefixed with 'Z'. (default in
* Xapian >= 1.3.1)
* - STEM_ALL: Stem all terms (note: no 'Z' prefix is added).
* - STEM_ALL_Z: Stem all terms (note: 'Z' prefix is added). (new in
* Xapian 1.2.11 and 1.3.1)
*/
void set_stemming_strategy(stem_strategy strategy);
/** Set the stopper.
*
* @param stop The Stopper object to set (default NULL, which means no
* stopwords).
*/
void set_stopper(const Stopper *stop = NULL);
/** Set the default operator.
*
* @param default_op The operator to use to combine non-filter
* query items when no explicit operator is used.
*
* So for example, 'weather forecast' is parsed as
* if it were 'weather OR forecast' by default.
*
* The most useful values for this are OP_OR (the
* default) and OP_AND. OP_NEAR, OP_PHRASE,
* OP_ELITE_SET, OP_SYNONYM and OP_MAX are also
* permitted. Passing other values will result in
* InvalidArgumentError being thrown.
*/
void set_default_op(Query::op default_op);
/** Get the current default operator. */
Query::op get_default_op() const;
/** Specify the database being searched.
*
* @param db The database to use for spelling correction
* (FLAG_SPELLING_CORRECTION), and synonyms (FLAG_SYNONYM,
* FLAG_AUTO_SYNONYMS, and FLAG_AUTO_MULTIWORD_SYNONYMS).
*/
void set_database(const Database &db);
/** Specify the maximum expansion of a wildcard and/or partial term.
*
* Note: you must also set FLAG_WILDCARD and/or FLAG_PARTIAL in the flags
* parameter to @a parse_query() for this setting to have anything to
* affect.
*
* If you don't call this method, the default settings are no limit on
* wildcard expansion, and partial terms expanding to the most frequent
* 100 terms - i.e. as if you'd called:
*
* set_max_expansion(0);
* set_max_expansion(100, Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT, Xapian::QueryParser::FLAG_PARTIAL);
*
* @param max_expansion The maximum number of terms each wildcard in the
* query can expand to, or 0 for no limit (which is the
* default).
* @param max_type @a Xapian::Query::WILDCARD_LIMIT_ERROR,
* @a Xapian::Query::WILDCARD_LIMIT_FIRST or
* @a Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT
* (default: Xapian::Query::WILDCARD_LIMIT_ERROR).
* @param flags What to set the limit for (default:
* FLAG_WILDCARD|FLAG_PARTIAL, setting the limit for both
* wildcards and partial terms).
*
* @since 1.3.3
*/
void set_max_expansion(Xapian::termcount max_expansion,
int max_type = Xapian::Query::WILDCARD_LIMIT_ERROR,
unsigned flags = FLAG_WILDCARD|FLAG_PARTIAL);
/** Specify the maximum expansion of a wildcard.
*
* If any wildcard expands to more than @a max_expansion terms, an
* exception will be thrown.
*
* This method is provided for API compatibility with Xapian 1.2.x and is
* deprecated - replace it with:
*
* set_max_wildcard_expansion(max_expansion,
* Xapian::Query::WILDCARD_LIMIT_ERROR,
* Xapian::QueryParser::FLAG_WILDCARD);
*/
XAPIAN_DEPRECATED(void set_max_wildcard_expansion(Xapian::termcount));
/** Parse a query.
*
* @param query_string A free-text query as entered by a user
* @param flags Zero or more QueryParser::feature_flag specifying
* what features the QueryParser should support. Combine
* multiple values with bitwise-or (|) (default FLAG_DEFAULT).
* @param default_prefix The default term prefix to use (default none).
* For example, you can pass "A" when parsing an "Author" field.
*
* @exception If the query string can't be parsed, then
* Xapian::QueryParserError is thrown. You can get an English
* error message to report to the user by catching it and
* calling get_msg() on the caught exception. The current
* possible values (in case you want to translate them) are:
*
* @li Unknown range operation
* @li parse error
* @li Syntax: <expression> AND <expression>
* @li Syntax: <expression> AND NOT <expression>
* @li Syntax: <expression> NOT <expression>
* @li Syntax: <expression> OR <expression>
* @li Syntax: <expression> XOR <expression>
*/
Query parse_query(const std::string &query_string,
unsigned flags = FLAG_DEFAULT,
const std::string &default_prefix = std::string());
/** Add a probabilistic term prefix.
*
* For example:
*
* @code
* qp.add_prefix("author", "A");
* @endcode
*
* This allows the user to search for author:Orwell which will be
* converted to a search for the term "Aorwell".
*
* Multiple fields can be mapped to the same prefix. For example, you
* can make title: and subject: aliases for each other.
*
* As of 1.0.4, you can call this method multiple times with the same
* value of field to allow a single field to be mapped to multiple
* prefixes. Multiple terms being generated for such a field, and
* combined with @c Xapian::Query::OP_OR.
*
* If any prefixes are specified for the empty field name (i.e. you
* call this method with an empty string as the first parameter)
* these prefixes will be used for terms without a field specifier.
* If you do this and also specify the @c default_prefix parameter to @c
* parse_query(), then the @c default_prefix parameter will override.
*
* If the prefix parameter is empty, then "field:word" will produce the
* term "word" (and this can be one of several prefixes for a particular
* field, or for terms without a field specifier).
*
* If you call @c add_prefix() and @c add_boolean_prefix() for the
* same value of @a field, a @c Xapian::InvalidOperationError exception
* will be thrown.
*
* In 1.0.3 and earlier, subsequent calls to this method with the same
* value of @a field had no effect.
*
* @param field The user visible field name
* @param prefix The term prefix to map this to
*/
void add_prefix(const std::string& field, const std::string& prefix);
/** Register a FieldProcessor.
*/
void add_prefix(const std::string& field, Xapian::FieldProcessor * proc);
/** Add a boolean term prefix allowing the user to restrict a
* search with a boolean filter specified in the free text query.
*
* For example:
*
* @code
* qp.add_boolean_prefix("site", "H");
* @endcode
*
* This allows the user to restrict a search with site:xapian.org which
* will be converted to Hxapian.org combined with any probabilistic
* query with @c Xapian::Query::OP_FILTER.
*
* If multiple boolean filters are specified in a query for the same
* prefix, they will be combined with the @c Xapian::Query::OP_OR
* operator. Then, if there are boolean filters for different prefixes,
* they will be combined with the @c Xapian::Query::OP_AND operator.
*
* Multiple fields can be mapped to the same prefix (so for example
* you can make site: and domain: aliases for each other). Instances of
* fields with different aliases but the same prefix will still be
* combined with the OR operator.
*
* For example, if "site" and "domain" map to "H", but author maps to "A",
* a search for "site:foo domain:bar author:Fred" will map to
* "(Hfoo OR Hbar) AND Afred".
*
* As of 1.0.4, you can call this method multiple times with the same
* value of field to allow a single field to be mapped to multiple
* prefixes. Multiple terms being generated for such a field, and
* combined with @c Xapian::Query::OP_OR.
*
* Calling this method with an empty string for @a field will cause
* a @c Xapian::InvalidArgumentError.
*
* If you call @c add_prefix() and @c add_boolean_prefix() for the
* same value of @a field, a @c Xapian::InvalidOperationError exception
* will be thrown.
*
* In 1.0.3 and earlier, subsequent calls to this method with the same
* value of @a field had no effect.
*
* @param field The user visible field name
* @param prefix The term prefix to map this to
* @param grouping Controls how multiple filters are combined - filters
* with the same grouping value are combined with OP_OR,
* then the resulting queries are combined with OP_AND.
* If NULL, then @a field is used for grouping. If an
* empty string, then a unique grouping is created for
* each filter (this is sometimes useful when each
* document can have multiple terms with this prefix).
* [default: NULL]
*/
void add_boolean_prefix(const std::string &field, const std::string &prefix,
const std::string* grouping = NULL);
/** Add a boolean term prefix allowing the user to restrict a
* search with a boolean filter specified in the free text query.
*
* This is an older version of this method - use the version with
* the `grouping` parameter in preference to this one.
*
* @param field The user visible field name
* @param prefix The term prefix to map this to
* @param exclusive Controls how multiple filters are combined. If
* true then @a prefix is used as the `grouping` value,
* so terms with the same prefix are combined with OP_OR,
* then the resulting queries are combined with OP_AND.
* If false, then a unique grouping is created for
* each filter (this is sometimes useful when each
* document can have multiple terms with this prefix).
*/
void add_boolean_prefix(const std::string &field, const std::string &prefix,
bool exclusive) {
if (exclusive) {
add_boolean_prefix(field, prefix);
} else {
std::string empty_grouping;
add_boolean_prefix(field, prefix, &empty_grouping);
}
}
/** Register a FieldProcessor for a boolean prefix.
*/
void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
const std::string* grouping = NULL);
/** Register a FieldProcessor for a boolean prefix.
*
* This is an older version of this method - use the version with
* the `grouping` parameter in preference to this one.
*/
void add_boolean_prefix(const std::string &field, Xapian::FieldProcessor *proc,
bool exclusive) {
if (exclusive) {
add_boolean_prefix(field, proc);
} else {
std::string empty_grouping;
add_boolean_prefix(field, proc, &empty_grouping);
}
}
/// Begin iterator over terms omitted from the query as stopwords.
TermIterator stoplist_begin() const;
/// End iterator over terms omitted from the query as stopwords.
TermIterator XAPIAN_NOTHROW(stoplist_end() const) {
return TermIterator();
}
/// Begin iterator over unstemmed forms of the given stemmed query term.
TermIterator unstem_begin(const std::string &term) const;
/// End iterator over unstemmed forms of the given stemmed query term.
TermIterator XAPIAN_NOTHROW(unstem_end(const std::string &) const) {
return TermIterator();
}
/// Register a RangeProcessor.
void add_rangeprocessor(Xapian::RangeProcessor * range_proc,
const std::string* grouping = NULL);
/** Register a ValueRangeProcessor.
*
* This method is provided for API compatibility with Xapian 1.2.x and is
* deprecated - use @a add_rangeprocessor() with a RangeProcessor instead.
*/
XAPIAN_DEPRECATED(void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc)) {
/// Compatibility shim.
class ShimRangeProcessor : public RangeProcessor {
Xapian::Internal::opt_intrusive_ptr<Xapian::ValueRangeProcessor> vrp;
public:
ShimRangeProcessor(Xapian::ValueRangeProcessor * vrp_)
: RangeProcessor(Xapian::BAD_VALUENO), vrp(vrp_) { }
Xapian::Query
operator()(const std::string &begin, const std::string &end)
{
std::string b = begin, e = end;
slot = (*vrp)(b, e);
if (slot == Xapian::BAD_VALUENO)
return Xapian::Query(Xapian::Query::OP_INVALID);
return RangeProcessor::operator()(b, e);
}
};
add_rangeprocessor((new ShimRangeProcessor(vrproc))->release());
}
/** Get the spelling-corrected query string.
*
* This will only be set if FLAG_SPELLING_CORRECTION is specified when
* QueryParser::parse_query() was last called.
*
* If there were no corrections, an empty string is returned.
*/
std::string get_corrected_query_string() const;
/// Return a string describing this object.
std::string get_description() const;
};
inline void
QueryParser::set_max_wildcard_expansion(Xapian::termcount max_expansion)
{
set_max_expansion(max_expansion,
Xapian::Query::WILDCARD_LIMIT_ERROR,
FLAG_WILDCARD);
}
/// @private @internal Helper for sortable_serialise().
XAPIAN_VISIBILITY_DEFAULT
size_t XAPIAN_NOTHROW(sortable_serialise_(double value, char * buf));
/** Convert a floating point number to a string, preserving sort order.
*
* This method converts a floating point number to a string, suitable for
* using as a value for numeric range restriction, or for use as a sort
* key.
*
* The conversion is platform independent.
*
* The conversion attempts to ensure that, for any pair of values supplied
* to the conversion algorithm, the result of comparing the original
* values (with a numeric comparison operator) will be the same as the
* result of comparing the resulting values (with a string comparison
* operator). On platforms which represent doubles with the precisions
* specified by IEEE_754, this will be the case: if the representation of
* doubles is more precise, it is possible that two very close doubles
* will be mapped to the same string, so will compare equal.
*
* Note also that both zero and -zero will be converted to the same
* representation: since these compare equal, this satisfies the
* comparison constraint, but it's worth knowing this if you wish to use
* the encoding in some situation where this distinction matters.
*
* Handling of NaN isn't (currently) guaranteed to be sensible.
*
* @param value The number to serialise.
*/
inline std::string sortable_serialise(double value) {
char buf[9];
return std::string(buf, sortable_serialise_(value, buf));
}
/** Convert a string encoded using @a sortable_serialise back to a floating
* point number.
*
* This expects the input to be a string produced by @a sortable_serialise().
* If the input is not such a string, the value returned is undefined (but
* no error will be thrown).
*
* The result of the conversion will be exactly the value which was
* supplied to @a sortable_serialise() when making the string on platforms
* which represent doubles with the precisions specified by IEEE_754, but
* may be a different (nearby) value on other platforms.
*
* @param serialised The serialised string to decode.
*/
XAPIAN_VISIBILITY_DEFAULT
double XAPIAN_NOTHROW(sortable_unserialise(const std::string & serialised));
}
#endif // XAPIAN_INCLUDED_QUERYPARSER_H
|