fork download
  1. /*
  2.  
  3. Copyright 2017 dbj@dbj.org
  4.  
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http ://www.apache.org/licenses/LICENSE-2.0
  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. */
  15.  
  16. #include <string>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <cstring>
  20. #include <functional>
  21.  
  22. #define DBJINLINE static inline
  23.  
  24. /*
  25. dbj added
  26. */
  27. template <unsigned Count, typename ... Args>
  28. DBJINLINE void Print(char const (&format)[Count],
  29. Args const & ... args)
  30. {
  31. if (!std::strstr(format, "%"))
  32. throw "Can not start Print() arguments, with a string which has no '%' (aka the placeholder) in it.";
  33. printf(format, args ...);
  34. }
  35.  
  36. // using std::string;
  37. typedef std::function<std::string()> TestUnit;
  38. typedef std::vector<TestUnit> AllUnits;
  39.  
  40. /* DBJ added
  41. find first s2 in s1
  42. return the position relative to the s1 begining
  43. return -1 if s2 not found in s1
  44. */
  45. template< typename S1, typename S2>
  46. auto find_first_of(const S1 & s1, const S2 & s2) {
  47. auto pos_ = std::find_first_of(
  48. std::begin(s1), std::end(s1),
  49. std::begin(s2), std::end(s2)
  50. );
  51.  
  52. return ( pos_ == std::end(s1) ? -1 : std::distance( std::begin(s1), pos_ ) );
  53. }
  54.  
  55. static AllUnits test_units = {
  56. [] {
  57. const char format[] = {"abra % ka % dabra"};
  58. const char placeholder[] = {"%"};
  59.  
  60. auto dbj = find_first_of(format, placeholder);
  61.  
  62. if (dbj < 0 )
  63. Print("Placeholder %s not found in %s", placeholder, format);
  64. else
  65. Print("Found placeholder \"%s\" in \"%s\", at position: %d", placeholder, format,
  66. static_cast<int>(dbj)
  67. );
  68.  
  69. return "OK: T1 -from->" __FILE__ ;
  70. }
  71. };
  72.  
  73. DBJINLINE void do_the_tests()
  74. {
  75. for (auto tunit : test_units) {
  76. Print("\n%", tunit().data());
  77. }
  78. }
  79.  
  80. #define DBJVERSION __DATE__ __TIME__
  81. #pragma message( "Compiling: " __FILE__ ", Version: " DBJVERSION)
  82. #pragma comment( user, "(c) 2017 by dbj@dbj.org code, Version: " DBJVERSION )
  83. int main() {
  84. do_the_tests();
  85. Print("%s",("\n\nDone! " DBJVERSION));
  86. return 0;
  87. }
  88.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Found placeholder "%" in "abra % ka % dabra", at position: 5


Done!  Apr  8 201716:12:50