1. C++ / Говнокод #14424

    +10

    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
    align.h: 
    #pragma once
    #include "stdint.h"
    namespace tblib 
    {
      template <typename T>
      struct check_align
      {
        uint8_t c;
        T t;
        check_align();
        check_align(const check_align&);
        check_align&operator=(const check_align&);
      };
    
      template <typename T>
      struct align_value
      {
        enum { value = sizeof(check_align<T>)-sizeof(T) };
      };
    
      template <int N>
      struct align_by_size;
    
      template <> 
      struct align_by_size<1> { uint8_t guts; };
    
      template <> 
      struct align_by_size<2> { uint16_t  guts; };
    
      template <> 
      struct align_by_size<4> { uint32_t guts; };
    
      template <> 
      struct align_by_size<8> { uint64_t guts; };
    
      template <typename T>
      struct align
      {
        align_by_size<align_value<T>::value> guts;
      };
    };
     класс массива (начало), файл tbarr.h: 
    #pragma once
    
    #include "stdint.h"
    #include "assert.h"
    #include <algorithm>
    #include "tbslice.h"
    #include "align.h"
    
    // FUCK THE EXCEPTIONS
    
    template <typename T>
    void construct (T* first, T* last) {
      while (first!=last)  {
        new(first) T;
        ++first;
      }
    }
    
    template <typename T>
    void destroy (T* first, T* last)  {
      while (last!=first)  {
        --last;
        last->~T();
      }
    }
    
    namespace tblib
    {  
      template <typename T, int N>
      class base_array
      {
        union
        {
          uint8_t memory [sizeof(T[N])];
          align<T> aligner;
        };
      public :

    мой выравниватель
    намного красивее бустовского, не так ли?

    TarasB, 27 Января 2014

    Комментарии (31)
  2. JavaScript / Говнокод #14423

    +155

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    var partner_id = location.href;
            //Определить значение четвертого сегмента
            var i = 1;
            while (i <= 5) {
                partner_id = partner_id.substr(partner_id.indexOf('/') + 1);
                i++;
            }
            var pos = partner_id.indexOf('/');
            if (pos > 0) {
                partner_id = partner_id.substr(0, pos);
            }

    Урлопарсинг от Папке.

    ragnar, 27 Января 2014

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

    +127

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    // ------- CLICKING STUFF IN CONTAINERS -------
    	if ((!( src in usr.contents ) && (((!( isturf(src) ) && (!( isturf(src.loc) ) && (src.loc && !( isturf(src.loc.loc) )))) || !( isturf(usr.loc) )) && (src.loc != usr.loc && (!( istype(src, /obj/screen) ) && !( usr.contents.Find(src.loc) ))))))
    		if (istype(usr, /mob/living/silicon/ai))
    			var/mob/living/silicon/ai/ai = usr
    			if (ai.control_disabled || ai.malfhacking)
    				return
    		else
    			return

    Взято из кода обработки нажатия одной опенсорсной онлайн-игрушки. Зачем, за что и почему?

    ACCount, 27 Января 2014

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

    −159

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    sub trim
    {
        my ($string) = @_;
        for ($string)
        {
            s/^\s+//;
            s/\s+$//;
        }
        return $string;
    }

    for ($string) такой for ($string)...

    http://ideone.com/JWu2Kt

    bormand, 27 Января 2014

    Комментарии (25)
  5. Perl / Говнокод #14420

    −159

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    if ("0 but true" == 0) {
        print "Zero! ";
    }
    if ("0 but true") {
        print "But true...";
    }

    The current default implementation always returns true without actually doing anything. Actually, it returns "0 but true" which is true but zero. That way you can tell if the return value is genuine or just the default.

    http://ideone.com/8FzrUI

    Zero! But true...

    bormand, 27 Января 2014

    Комментарии (43)
  6. PHP / Говнокод #14419

    +165

    1. 1
    2. 2
    3. 3
    4. 4
    if (($pos = strpos($_SERVER["REQUEST_URI"], "?")) !== false)
    {
    	$params = substr($_SERVER["REQUEST_URI"], $pos+1);
    	parse_str($params, $_GET);

    Bitrix, 404.php

    Посоны не слышали про $_SERVER[QUERY_STRING], зато, сука, неймспейсы знают!

    Boolean, 26 Января 2014

    Комментарии (12)
  7. Java / Говнокод #14418

    +66

    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
    @NamedQuery(
        		name = "ImageBankTag.findAllTags",
        		query = "select distinct tag from AdTag tag where tag.tag in (:tags) order by tag.tag"
        )
    })
    
    @Data
    @Entity
    @Table(name = "image_bank_tags",uniqueConstraints = {@UniqueConstraint(columnNames={"tag"})})
    public class ImageBankTag implements Serializable{
    
    . . .
    
        @NamedQuery(
        		name = "AdTag.findAllTags",
        		query = "select distinct tag from AdTag tag where tag.tag in (:tags) order by tag.tag"
        )
    })
    
    @Data
    @Entity
    @Table(name = "admanager2_tags",uniqueConstraints = {@UniqueConstraint(columnNames={"tag"})})
    public class AdTag implements Serializable{

    Такой вот своеобразный джоин, с подстраховкой.

    wvxvw, 26 Января 2014

    Комментарии (12)
  8. Java / Говнокод #14417

    +75

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    ObservableStorage.ConfigurationTuple<Integer, ObservableStorage.ConfigurationTuple<Foo, ObservableStorage.ConfigurationTuple<Foo, ObservableStorage.ConfigurationEnd>>> build = RemoteObservableStorage
            .configurationBuilder()
            .use("foo").as(Foo.class)
            .use("foo1").as(Foo.class)
            .use("ids").as(Integer.class)
            .build();

    И тут Остапа понесло :)
    Всем тайп сейфити посоны

    myzone, 26 Января 2014

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

    +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
    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
    struct Vector2f{float x, y;};
    struct Vector3f{float x, y, z;};
    struct Tensor3f{float xx, xy, xz, yy, yz, zz;};
    struct Matrix3x3f{float data[9];};
    struct Space2
    {
      typedef Vector2f Vector;
    };
    struct Space3
    {
      typedef Vector3f Vector;
    };
    
    
    template<typename Space>
    struct ParticleSystem
    {
      template<typename T>
      struct ParticleData{};
    
      template<> 
      struct ParticleData<Space2>
      {
        float orientation;
        float invInertia;
      };
    
      template<> 
      struct ParticleData<Space3>
      {
        typename Matrix3x3f orientation;
        typename Tensor3f inertiaTensor;
      };
    
      struct Particle : public ParticleData<Space>
      {
        typename Space::Vector pos, velocity;
      };
    
      template<typename T>
      void DumpParticle(){}
    
      template<>
      void DumpParticle<Space2>()
      {
        printf("%f %f", particles[0].orientation, particles[0].invInertia);
      }
    
      template<>
      void DumpParticle<Space3>()
      {
        printf("%f %f", particles[0].orientation.data[0], particles[0].inertia.xx);
      }
      
      void DumpParticles()
      {
        DumpParticle<Space>();
      }
      std::vector<Particle> particles;
    };

    Хочу объединить трехмерный и двухмерный движок.

    LispGovno, 26 Января 2014

    Комментарии (50)
  10. C++ / Говнокод #14415

    +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
    #include <iostream>
    using namespace std;
    
        void enable_misalignment_access_check(){
          cout<<"begin "<<__FUNCTION__<<endl;
          __asm__(
            "pushf\n"
            "orl $(1<<18),(%esp)\n"
            "popf\n"
          );
          cout<<"end "<<__FUNCTION__<<endl;
        }
    
    void alignedAccess(volatile unsigned char foo[])
    {
      cout<<"begin "<<__FUNCTION__<<endl;
      volatile int t = *(int *)(foo);
      cout<<"end "<<__FUNCTION__<<endl;
    }
    
    void unalignedAccess(volatile unsigned char foo[])
    {
      cout<<"begin "<<__FUNCTION__<<endl;
      volatile int t = *(int *)(foo+1);
      cout<<"end "<<__FUNCTION__<<endl;
    }
    
    unsigned char foo[] = { 1, 2, 3, 4, 5, 6 };
    
    int main(void)
    {
        alignedAccess(foo);
        unalignedAccess(foo);
        enable_misalignment_access_check();
        alignedAccess(foo);
        unalignedAccess(foo);
        return 0;
    }

    http://codepad.org/D6b5asES

    begin alignedAccess end alignedAccess 
    begin unalignedAccess end unalignedAccess
    begin enable_misalignment_access_check end enable_misalignment_access_check
    begin alignedAccess end alignedAccess
    begin unalignedAccess
    Bus error

    LispGovno, 26 Января 2014

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