Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Beautiful Code

Name: Anonymous 2008-10-02 19:21

Sup /prog/-riders
In this thread we post beautiful code. They can be from your job, school, whatever.

Here's a recursive regex parser with support for character classes, $ and & anchors, and the . and * metachars.


int matchhere(char* regexp, char* text);
int matchstar(int c, char* regexp, char* text)
{
    do {
        if (matchhere(regexp, text))
            return 1;
    } while (*text != '\0' && (*text++ == c || c == '.'));
    return 0;
}

int matchhere(char* regexp, char* text)
{
    if (regexp[0] == '\0')
        return 1;
    if (regexp[1] == '*')
        return matchstar(regexp[0], regexp+2, text);
    if (regexp[0] == '$' && regexp[1] == '\0')
        return *text == '\0';
    if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
        return matchhere(regexp+1, text+1);
    return 0;
}

int match(char* regexp, char* text)
{
    if (regexp[0] == '^')
        return matchhere(regexp+1, text);
    do {
        if (matchhere(regexp, text))
            return 1;
    } while (*text++ != '\0');
    return 0;
}

Name: Anonymous 2008-10-02 19:22

that should be $ and ^ anchors, my bad.

Name: Anonymous 2008-10-02 19:53

Tests whether a chain of nodes is cyclic using the "racing pointers" algorithm.


bool is_cyclic(node *start)
{
    node *slow = start, *fast = start;

    if(start && start->next) do {
        slow = slow->next;
        fast = fast->next->next;
    } while(fast && slow && fast->next && slow->next && slow != fast);

    return start && slow == fast;
}

Name: Anonymous 2008-10-02 20:21

>>3
Unnecesary checks on the slow pointer; return rechecks conditions.

int is_cyclic(node *fast)
{
    if(!fast) return 0;

    node *slow = fast;

    for(;;) {
        fast = fast->next;
        if(!fast) return 0;

        fast = fast->next;
        if(!fast) return 0;

        slow = slow->next;

        if(slow == fast) return 1;
    }
}

Name: Anonymous 2008-10-02 20:38

>>1
Doesn't support char classes, or at least I don't see a ']' or '[' anywhere.

Core of comm:

  res=memcmp(l1.data,l2.data,(l1.used < l2.used)?l1.used+1:l2.used+1);
  if(!res) { /* line in both files */
   if(cols&4 && l1.used) {
    if(cols&2) putchar('\t');
    if(cols&1) putchar('\t');
    fwrite(l1.data,1,l1.used,stdout);
   }
   need=3;
  } else if(res<0) { /* line only in file1 */
   if(cols&1)
    fwrite(l1.data,1,l1.used,stdout);
   need=1;
  } else { /* line only in file2 */
   if(cols&2) {
    if(cols&1) putchar('\t');
    fwrite(l2.data,1,l2.used,stdout);
   }
   need=2;
  }

Name: Anonymous 2008-10-02 20:52

>>5
Yeah typo on my part, I was hoping nobody would notice.

Name: Anonymous 2008-10-02 21:45

>>4
this thread's for Beautiful Code.

Get your ugly double semi colons out of here.

Name: Anonymous 2008-10-02 21:55

>>7
;_;

Name: Anonymous 2008-10-02 22:46


$_="=]=>%-|{<-|}<&|`{";
tr~ -/:-@[-`{-}~`-{/" -~;
eval $_;


Been posted a couple of times on /prog/
Prints out READ SICP TODAY!!

Name: Anonymous 2008-10-02 23:08

>>9
Just for shits and giggles, run it as root.

Name: Anonymous 2008-10-02 23:19

>>1
I hope you're trolling and not actually that fucking stupid.

Apart from not really working (match abTTabTT1 against ab*ab*1), there is nothing even remotely beautiful in this code.

Name: Anonymous 2008-10-02 23:45

>>11
ITT we fail to understand how the * operator works.

Name: Anonymous 2008-10-02 23:47

>>11
I'd like to see you write a more compact and elegant regex parser in C, using no external libraries.

Name: Anonymous 2008-10-02 23:51

>>9
$ perl prog.pl
READ SICP TODAY!!

$

Hey, that's pretty neat. I wonder how it works.

Name: Anonymous 2008-10-03 0:02

>>13
If it doesn't fucking handle all the basic operations like alternation you cannot call your pile of shit a regex parser.

Name: Anonymous 2008-10-03 0:13

>>15
In my experience, ^, $, . and * is enough to cover 99% of use cases. Besides, the purpose wasn't to demonstrate anything usable, just something which is clear, concise and elegant. If you can't see how a recursive regex parser in 21 C statements doesn't meet this criteria, then just sage and begone.
Otherwise, I eagerly await your contribution to this thread, if you already haven't done so.

Name: Anonymous 2008-10-03 0:23

ITT we compliment our own shitty code.

No code snippet from me because I am man enough to openly admit that my code is shit.

Name: Anonymous 2008-10-03 0:27

>>17
No, that makes you a defensive coward.
You seem like the type of person who is reluctant to participate in anything, due to an irrational fear of being hurt. What are you afraid of, what are you running away from?
Yourself.

Name: Anonymous 2008-10-03 0:44

>>16
less than POSIX basic regular expressions = shit.

Name: Anonymous 2008-10-03 1:07

>>13,16,18
ITT: butthurt faggots get overly defensive because anonymous ``programmers" don't appreciate useless code

Name: Anonymous 2008-10-03 1:16

>>16
Uh, regexes are all about grouping. Your trivial program is absolutely useless, and I don't want to know what you experienced if it covers 99% of your needs.

Name: Anonymous 2008-10-03 1:20

>>9,14
guys, I'm running windows and don't have rm.

Name: Anonymous 2008-10-03 1:22

>>22
rot13 = map (find charMap)
  where find []     char = char
        find (c:cs) char | (fst c) == char = snd c
        find (c:cs) char | (snd c) == char = fst c
        find (_:cs) char = find cs char
        charMap = zip (['a'..'m'] ++ ['A'..'M'] ++ ['0'..'4'])
                      (['n'..'z'] ++ ['N'..'Z'] ++ ['5'..'9'])

Name: Anonymous 2008-10-03 1:23

>>19-21,17,11
Same person, trolled constantly.

Name: Anonymous 2008-10-03 2:50

for(char*r=s;*s;*r=!s++)if(*s!=c)*r++=*s;

Name: Anonymous 2008-10-03 3:04

>>25
for(char*r=s;*s;*r=!s++)*s^c&&*r++=*s;

Name: Anonymous 2008-10-03 4:32

  def insert(self):
    post_values = [_mysql.escape_string(str(value)) for key, value in self.post.iteritems()]
     
    db.query("INSERT INTO `posts` (`%s`) VALUES ('%s')" % (
      "`, `".join(self.post.keys()),
      "', '".join(post_values)
    ))

    return db.insert_id()

Name: Anonymous 2008-10-03 4:49

>>27
>_mysql

Name: Anonymous 2008-10-03 5:46

>>27
Maybe you should tell us what part of this is beautiful

Name: Anonymous 2008-10-03 6:28

>>29
Isn't it obvious?

Name: Anonymous 2008-10-03 6:30


strcpy(to, from, count)
char *to, *from;
int count;
{
    int n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
               } while (--n > 0);
    }
}

Name: Anonymous 2008-10-03 7:09

(loop (print (eval (read))))

Name: Anonymous 2008-10-03 9:09

>>30
No.

Name: Anonymous 2008-10-03 9:11

>>31
I always rage when I see this with *to++

Name: Anonymous 2008-10-03 10:28

>>31
My compiler does that for me.

Name: Anonymous 2008-10-03 19:24

>>31
``Duff's Device'' is not beautiful in the least.

Name: Anonymous 2008-10-03 21:00

this is a recursive getline that I did for this bookgoogle I had to do implementing a left leaning red-black tree in C

I did this function to get the lines of the book using malloc just once, with the exact size of the line...


char *getLineR(FILE *in_file, char *line, int n)
{
  char c = fgetc(in_file);
  if (c != '\n') {
    line = getLineR(in_file, line, n + 1);
  }
  else {
    line = malloc((n+2)*sizeof(char));
    if (line == NULL) exit(1);
    line[(n+1)] = '\0';
  }
  line[n] = c;
  return line;
}


it's beautiful, works and I'm proud of it

oh, and in the same program I did a faster tolower() to get my chars in lowercase, since I already knew they were letters, I didn't need to check if they were (the original tolower from the ctype does that), so I did this:

#define my_tolower(A) (((A) % 32) + 96)

Name: Anonymous 2008-10-03 21:04

>>37
actually I just thought a even faster to lower
#define my_tolower(A) (((A)<<5 >>5) + 96)

Name: Anonymous 2008-10-03 21:06

>>38
#define my_tolower(A) (((A)<<5 >>5) + ('a' - 1))
more 'readable'

Name: Anonymous 2008-10-03 22:09

>>38,39
_tolower

Name: Anonymous 2008-10-03 22:47

>>40
_lower

Name: Anonymous 2008-10-04 1:18

__LOWER__

Name: Anonymous 2008-10-04 1:54

>>37
>>38
>>39
if you already know they're letters, then use this:
#define my_tolower(A) ((A)|32)

Name: Anonymous 2008-10-04 2:07

>>38-39
too bad it doesn't work.

Name: Anonymous 2008-10-04 2:21

>>44
What are you talking about?  It supports all 8 ASCII characters and successfully converts them to lowercase letters 7 out of 8 times!

Name: Anonymous 2008-10-04 2:54

ITT: People who think America is the whole world.

Name: Anonymous 2008-10-04 3:13

>>46
It's not the whole world but it is the standard.

Name: Anonymous 2008-10-04 3:50

>>46
i did the getline and I'm brazilian
o:

Name: Anonymous 2008-10-04 4:38

>>47
>>48
Eat UTF and die.

Name: Anonymous 2008-10-04 5:28

if OP thinks that code is beatufil they are fucking clueless

Name: Anonymous 2008-10-04 5:38

ITT IDIOCY

Name: Anonymous 2008-10-04 6:47

ITT Amerifags

Name: Anonymous 2008-10-04 10:07

ITB fags

Name: Anonymous 2008-10-04 10:08

IHBTC

Name: Anonymous 2008-10-05 3:19

>>40
it doesnt work

Name: Anonymous 2008-10-06 8:38


function buildTree($f)
{
    $tree = array();
    foreach ($f as $k => $z) {
        $tree[$k] = array();
    }
    foreach ($f as $k => $v) {
        $tree[$v['parent']][$k] = &$tree[$k];
    }
    return $tree;
}

Name: ​​​​​​​​​​ 2010-09-07 19:40


Don't change these.
Name: Email:
Entire Thread Thread List