This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils, streams, unsigned | |
# implement dan bernsteins cdb. | |
# utility | |
iterator mitems[T](a: var openarray[T]): var T = | |
for i in a.low..a.high: | |
yield a[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "pp" | |
def cdb_hash(s) | |
r = 5381 | |
# & 0xffffffff forces to 32 bit. (lots of hair for that) | |
s.split("").each { |c| r = (((r << 5) + r) ^ c.ord) & 0xffffffff } | |
return r | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unsigned | |
type hp = array[0..1,uint32] | |
var | |
cells: seq[hp] = @[] | |
ncells = 3 | |
cells.add( [uint32(1),uint32(0)] ) | |
echo repr(cells) | |
# => 0x7f8197d0b050[[(invalid data!), (invalid data!)]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var buckets: array[0..9, string] | |
# initialize. wish they were automagically initialized | |
for i in 0..buckets.high: buckets[i] = "" | |
# XXX not work | |
for i in 0..1: | |
for buck in buckets: | |
# following line doesn't work. compile error: | |
# Error: for a 'var' type a variable needs to be passed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
# use mod == 0 a lot... | |
proc mod0(a,b:int):bool = (a mod b) == 0 | |
# if i try to add to "result" directly I get a SIGSEGV | |
# so use "res" instead. why? | |
# oh yeah and there's probably a much better way to do this... | |
proc addCommas(s:string): string = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r = 0 | |
var tmp:int32 = cast[int32](pkt.len) | |
var thenum:int32 = 0 | |
bigEndian32(addr(thenum), addr(tmp)) | |
echo repr(addr(thenum)) | |
r = s.send(addr(thenum), 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# length of data in 32-bit unsigned, network (big-endian) byte order | |
# plus the data (and increase length by 4 to account for length field | |
def packed(xml) | |
[xml.size + 4].pack("N") + xml | |
end | |
puts packed("a test packet") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nimrod c -d:ssl connect.nim | |
import sockets, strutils | |
let defaultSSLContext = newContext(verifyMode = CVerifyNone) | |
var | |
s = socket() | |
server = "www.google.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nimrod c -d:ssl connect.nim | |
# connect.nim(12, 4) Error: undeclared identifier: 'isSSL=' | |
import sockets, strutils | |
var | |
sock: TSocket | |
server = "www.google.com" | |
port = 443 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUFSIZE 8*1024 | |
int main() | |
{ | |
FILE *fp = stdin; /* or use fopen to open a file */ | |
char buf[BUFSIZE+1]; | |
unsigned long c, n = 0; | |
int i, chars_read; |