check_op.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "y_absl/log/internal/check_op.h"
  15. #include <string.h>
  16. #include <ostream>
  17. #include "y_absl/strings/string_view.h"
  18. #ifdef _MSC_VER
  19. #define strcasecmp _stricmp
  20. #else
  21. #include <strings.h> // for strcasecmp, but msvc does not have this header
  22. #endif
  23. #include <sstream>
  24. #include <util/generic/string.h>
  25. #include "y_absl/base/config.h"
  26. #include "y_absl/strings/str_cat.h"
  27. namespace y_absl {
  28. Y_ABSL_NAMESPACE_BEGIN
  29. namespace log_internal {
  30. #define Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(x) \
  31. template TString* MakeCheckOpString(x, x, const char*)
  32. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(bool);
  33. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(int64_t);
  34. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(uint64_t);
  35. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(float);
  36. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(double);
  37. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(char);
  38. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(unsigned char);
  39. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const TString&);
  40. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const y_absl::string_view&);
  41. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const char*);
  42. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const signed char*);
  43. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const unsigned char*);
  44. Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const void*);
  45. #undef Y_ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING
  46. CheckOpMessageBuilder::CheckOpMessageBuilder(const char* exprtext) {
  47. stream_ << exprtext << " (";
  48. }
  49. std::ostream& CheckOpMessageBuilder::ForVar2() {
  50. stream_ << " vs. ";
  51. return stream_;
  52. }
  53. TString* CheckOpMessageBuilder::NewString() {
  54. stream_ << ")";
  55. return new TString(stream_.str());
  56. }
  57. void MakeCheckOpValueString(std::ostream& os, const char v) {
  58. if (v >= 32 && v <= 126) {
  59. os << "'" << v << "'";
  60. } else {
  61. os << "char value " << int{v};
  62. }
  63. }
  64. void MakeCheckOpValueString(std::ostream& os, const signed char v) {
  65. if (v >= 32 && v <= 126) {
  66. os << "'" << v << "'";
  67. } else {
  68. os << "signed char value " << int{v};
  69. }
  70. }
  71. void MakeCheckOpValueString(std::ostream& os, const unsigned char v) {
  72. if (v >= 32 && v <= 126) {
  73. os << "'" << v << "'";
  74. } else {
  75. os << "unsigned char value " << int{v};
  76. }
  77. }
  78. void MakeCheckOpValueString(std::ostream& os, const void* p) {
  79. if (p == nullptr) {
  80. os << "(null)";
  81. } else {
  82. os << p;
  83. }
  84. }
  85. // Helper functions for string comparisons.
  86. #define DEFINE_CHECK_STROP_IMPL(name, func, expected) \
  87. TString* Check##func##expected##Impl(const char* s1, const char* s2, \
  88. const char* exprtext) { \
  89. bool equal = s1 == s2 || (s1 && s2 && !func(s1, s2)); \
  90. if (equal == expected) { \
  91. return nullptr; \
  92. } else { \
  93. return new TString( \
  94. y_absl::StrCat(exprtext, " (", s1, " vs. ", s2, ")")); \
  95. } \
  96. }
  97. DEFINE_CHECK_STROP_IMPL(CHECK_STREQ, strcmp, true)
  98. DEFINE_CHECK_STROP_IMPL(CHECK_STRNE, strcmp, false)
  99. DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)
  100. DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)
  101. #undef DEFINE_CHECK_STROP_IMPL
  102. namespace detect_specialization {
  103. StringifySink::StringifySink(std::ostream& os) : os_(os) {}
  104. void StringifySink::Append(y_absl::string_view text) { os_ << text; }
  105. void StringifySink::Append(size_t length, char ch) {
  106. for (size_t i = 0; i < length; ++i) os_.put(ch);
  107. }
  108. void AbslFormatFlush(StringifySink* sink, y_absl::string_view text) {
  109. sink->Append(text);
  110. }
  111. } // namespace detect_specialization
  112. } // namespace log_internal
  113. Y_ABSL_NAMESPACE_END
  114. } // namespace y_absl