1. PHP / Говнокод #23185

    +4

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    // Consumer
    $channel->run(
        new GracefulHandler(
            new LoggingHandler(
                new AcknowledgingHandler(
                    new JsonDecodingHandler(
                        new ValidatingHandler(
                            new DbalReconnectingHandler(
                                new AmqpPublishingHandler(
                                    new Mailman($channel, $exchangeName),
                                    new PrintNodePrinterPool(
                                        new PluginClient(
                                            new HttpClient(),
                                            [new AuthenticationPlugin(new BasicAuth($key, ''))]
                                        ),
                                        MessageFactoryDiscovery::find()
                                    ),
                                    new DbalBatchFactory(
                                        $connection,
                                        new S3BatchLabelFactory(
                                            new S3Client(
                                                [
                                                    'version' => 'latest',
                                                    'region' => $region,
                                                    'credentials' => [
                                                        'key' => $key,
                                                        'secret' => $secret,
                                                    ],
                                                ]
                                            ),
                                            $bucket,
                                            $logger
                                        ),
                                        new LabelFactory(
                                            new GuzzleClient(),
                                            new PickTicketFactory($template, $layouts)
                                        )
                                    )
                                ),
                                $connection
                            ),
                            __DIR__ . '/schema.json'
                        )
                    )
                ),
                $logger
            )
        ),
        $name
    );

    Пример consumer на PHP для RabbitMQ (Yegor OOP style)

    belka3000, 14 Июля 2017

    Комментарии (7)
  2. C# / Говнокод #23184

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    public bool CheckBool(string value)
           {
               value = value.ToLower();
               return !string.IsNullOrEmpty(value) && (value == "on" || value == "yes" || value == "1") ? true : false;
           }

    another variant ;D

    selicate, 14 Июля 2017

    Комментарии (4)
  3. C# / Говнокод #23183

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    static string[] nums = new string[60] {
                "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
                "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"
            };
    
            /// <summary>
            /// Форматирует дату в dd.MM.yyyy
            /// </summary>
            /// <param name="date"></param>
            /// <returns></returns>
            public static string ShortRuDateStr(ref DateTime date)
            {
                return string.Concat(nums[date.Day], ".", nums[date.Month], ".", YearToString(date.Year));
            }
    
    
            /// <summary>
            /// Возвращает текущую дату в формате yyyyMMddHHmmss
            /// </summary>
            /// <returns></returns>
            public static string Timestamp(ref DateTime date)
            {
                return string.Concat(YearToString(date.Year), nums[date.Month], nums[date.Day], nums[date.Hour], nums[date.Minute], nums[date.Second]);
            }
    
            public static string YearToString(int year)
            {
                return year.ToString().PadLeft(4, '0');
            }

    michael443959, 13 Июля 2017

    Комментарии (20)
  4. PHP / Говнокод #23182

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    public function notLessThan($attribute_name, $params)
        {
            $attributes = [
                [
                    'error' => 0,
                    'name' => 'ingredient_1st',
                    'this' => $this->ingredient_1st
                ],
                [
                    'error' => 0,
                    'name' => 'ingredient_2nd',
                    'this' => $this->ingredient_2nd,
                ],
                [
                    'error' => 0,
                    'name' => 'ingredient_3rd',
                    'this' => $this->ingredient_3rd
                ],
                [
                    'error' => 0,
                    'name' => 'ingredient_4th',
                    'this' => $this->ingredient_4th
                ],
                [
                    'error' => 0,
                    'name' => 'ingredient_5th',
                    'this' => $this->ingredient_5th
                ],
            ];
            $this->counter = 0;
            foreach ($attributes as $key => &$attribute) { // change array element
                if (empty($attribute['this']) && $this->counter < 2) {
                    $attribute['error'] = 1;
                } else {
                    $this->counter++;
                }
            }
            if ($this->counter < 2) {
                foreach ($attributes as $key => $attribute) {
                    if ($attribute['error']) {
                        $this->addError($attribute['name'], 'Должно быть заполнено не менее двух полей.');
                    }
    
                }
                return false;
            }
            return true;
        }

    Валидатор проверки заполнения нужного количества полей

    qstd, 13 Июля 2017

    Комментарии (0)
  5. C++ / Говнокод #23181

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    #include <type_traits>
    
    struct TPred1 {};
    struct TPred2 {};
    template<typename P1, typename P2> struct TAnd1: P1, P2 {TAnd1(P1, P2) {}};
    template<typename P1, typename P2> struct TAnd2: P1, P2 {TAnd2(P1, P2) {}};
    
    template<typename T, T v> struct my_integral_constant {enum {value=v};};
    template<class T, class From> struct my_is_base_of:
      my_integral_constant<bool, __is_base_of(T, From)> {};
    
    template<typename P1, typename P2> std::enable_if_t<
      my_is_base_of<TPred1, P1>::value &&
      my_is_base_of<TPred1, P2>::value,
    TAnd1<P1, P2>> operator&&(P1 p1, P2 p2)
    {return {p1, p2};}
    
    template<typename P1, typename P2> std::enable_if_t<
      my_is_base_of<TPred2, P1>::value &&
      my_is_base_of<TPred2, P2>::value,
    TAnd2<P1, P2>> operator&&(P1 p1, P2 p2)
    {return {(p1, p2};}
    
    
    template<typename T> struct is_callable
    {
      template<typename T1> struct dummy;
      template<typename CheckType> static short check(dummy<decltype(
        std::declval<std::remove_reference_t<CheckType>>()())>*);
      template<typename CheckType> static char check(...);
      enum: bool {value = sizeof(check<T>(nullptr)) == sizeof(short)};
    };
    
    struct IntellisenseKiller
    {
      template<typename T, typename = std::enable_if_t<
        std::is_function<T>::value &&
        is_callable<T>::value
      >> IntellisenseKiller(const T&) {}
      IntellisenseKiller(bool) {}
    };
    IntellisenseKiller eat4GbRam = true;

    Обнаружился ещё один способ превратить Visual Studio 2017 в тормозной, пожирающий 4 ГБ оперативы, блокнот с подсветкой синтаксиса.
    Это минимальный пример, который я выделил из своей библиотеки и порезал всё, что не вызывает баг. Вроде бы не связанные между собой куски кода, но глюки студии вызывают только вместе. Целый день потратил на поиск причины глюка.
    Случайно обнаружил, что длина получившегося фрагмента кода 42 строки. Я не подгонял, честно. Так что можно сказать, что ошибка в 42 строке. Без этой строки Intellisense заработает.

    gammaker, 12 Июля 2017

    Комментарии (45)
  6. Си / Говнокод #23180

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    #include <stdint.h>
    #include <stdio.h>
    
    char a[] = {35,105,110,99,108,117,100,101,32,60,115,116,100,105,110,116,46,104,62,10,35,105,110,99,108,117,100,101,32,60,115,116,100,105,111,46,104,62,10,10,99,104,97,114,32,97,91,93,32,61,32,123,0,125,59,10,10,105,110,116,32,109,97,105,110,40,118,111,105,100,41,10,123,10,32,32,105,110,116,32,98,59,10,32,32,98,32,61,32,112,114,105,110,116,102,40,34,37,115,34,44,32,97,41,59,10,32,32,102,111,114,32,40,99,104,97,114,32,42,105,32,61,32,97,59,32,105,32,60,32,97,32,43,32,115,105,122,101,111,102,40,97,41,59,32,105,43,43,41,10,32,32,123,10,32,32,32,32,112,114,105,110,116,102,40,34,37,105,44,34,44,32,42,105,41,59,10,32,32,125,10,32,32,112,114,105,110,116,102,40,34,37,115,34,44,32,97,43,98,43,49,41,59,10,32,32,114,101,116,117,114,110,32,48,59,10,125,10,};
    
    int main(void)
    {
      int b;
      b = printf("%s", a);
      for (char *i = a; i < a + sizeof(a); i++)
      {
        printf("%i,", *i);
      }
      printf("%s", a+b+1);
      return 0;
    }

    куайн на сишечке

    j123123, 12 Июля 2017

    Комментарии (4)
  7. Куча / Говнокод #23179

    −3

    1. 1
    2. 2
    Камерунские нигеры ебут админа этого сайта в четыре ствола; но, что любопытно, нельзя сказать, чтобы это ему как-то прямо уж так-таки и не нравилось.
    Извиваться-то ужом он, положим, извивается; да только вопли из его глотки летят далеко не страдальческие...

    AnalPerOral, 12 Июля 2017

    Комментарии (4)
  8. C++ / Говнокод #23178

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    #include <algorithm>
    #include <exception>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>
    #include <random>
    #include <chrono>
    #define __CL_ENABLE_EXCEPTIONS
    #include "cl.hpp"
    
    using namespace std::string_literals;
    
    
    int main(int argc, char * argv[]) {
      
      
      size_t data_len = (1024 * 1024 * strtoul(argv[1], nullptr, 10)) / sizeof(uint32_t),
             out_len = strtoul(argv[2], nullptr, 10),
             iter = strtoul(argv[3], nullptr, 10),
             block_size = strtoul(argv[4], nullptr, 10);
      
      std::string src = R"(
        typedef unsigned int uint32_t;
    __kernel void bench(global const uint32_t * data, global uint32_t * out) {
        
        uint32_t res = 0, id = get_global_id(0), next = id;
        for(uint32_t i = 0; i < )"s + std::to_string(iter) + R"(; ++i) {
            for(uint32_t j = 0; j < )" + std::to_string(block_size / sizeof(uint32_t)) +  R"(; ++j)
                res ^= data[next + j];
            next = data[next];
        }
        out[id] = res;
        
    }
      
      )"s;
      
      cl::Program::Sources sources = {{src.data(), src.size()}};
    
    
      std::vector<cl::Platform> platforms;
      cl::Platform::get(&platforms);
      
      std::vector<uint32_t> data(data_len);
      std::vector<uint32_t> out(out_len);
      
      std::generate(std::begin(data), std::end(data), [=,gen = std::mt19937{}]() mutable {
        return gen() % (data_len - (block_size / sizeof(uint32_t)));
      });
      
      for(auto & platform : platforms) {
        std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << "\n";
        std::vector<cl::Device> devices;
        platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
        for(auto & device : devices) {
          try {
            std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << "\n";
            cl::Context ctx({device});
            cl::Program program(ctx, sources);
            program.build({device});
            
            cl::Buffer data_buffer(ctx, CL_MEM_READ_WRITE, data.size() * sizeof(uint32_t));
            cl::Buffer out_buffer(ctx, CL_MEM_READ_WRITE, out.size() * sizeof(uint32_t));
            cl::CommandQueue queue(ctx, device);
            queue.enqueueWriteBuffer(data_buffer, CL_TRUE, 0, data.size() * sizeof(uint32_t), data.data());
            
            cl::make_kernel<cl::Buffer &, cl::Buffer &> bench(program, "bench");
            cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(out.size()),cl::NullRange);
            
            auto start = std::chrono::high_resolution_clock::now();
            bench(eargs, data_buffer, out_buffer).wait();
            auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start).count();
            
            size_t ops = out_len * iter;
            size_t total_tp = ops * block_size;
            double miops = (ops / time) / (1000 * 1000);
            double tpgbps = (total_tp / time) / (1024 * 1024 * 1024);
            fprintf(stderr, "Result: %.2fMIOPS, %.2fGB/s, %.2fsec\n", miops, tpgbps, time);
            
            
            queue.enqueueReadBuffer(out_buffer, CL_TRUE, 0, out.size() * sizeof(uint32_t), out.data());   
          
          } catch(cl::Error e) {
            std::cout << e.what() << " : " << e.err() << std::endl;
            std::terminate();
          }
        }
        
      }
    }

    Код Царя
    https://www.linux.org.ru/forum/development/13489159
    https://github.com/superhackkiller1997/gpu_mem_benchmark/blob/master/main.cpp

    j123123, 12 Июля 2017

    Комментарии (59)
  9. C# / Говнокод #23177

    −1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    private bool CheckBool(string value)
           {
               if (value == "on")
                   return true;
               else
                   return false;
           }

    ////

    selicate, 12 Июля 2017

    Комментарии (2)
  10. Си / Говнокод #23176

    +5

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    inline int ms_001(int x){ return    x                                                ;}    //  x * 1
    inline int ms_002(int x){ return    x<<1                                             ;}    //  x * 2  
    inline int ms_003(int x){ return    x<<2 - x                                         ;}    //  x * 3  
    ...
    inline int ms_799(int x){ return    x<<10 - x<<8 + x<<5 - x                          ;}    //  x * 799
    inline int ms_800(int x){ return    x<<10 - x<<8 + x<<5                              ;}    //  x * 800
    
    // массив указателей 
      int ( *mult_shift[800] ) (int) = {
                                            ms_001,              
                                            ms_002,
    ...
                                            ms_799,
                                            ms_800  };

    Очень быстрое целочисленное умножение

    fse, 12 Июля 2017

    Комментарии (13)