Skip to content

Instantly share code, notes, and snippets.

@G4MR
G4MR / gist:4027d7b17ccf64992b34
Created May 9, 2014 17:56
Valitron Validator - Laravel like validation rules, also a better validateMin callback
class Validation extends \Valitron\Validator
{
/**
* Better validateMin check
* @param [string] $field [input name]
* @param [mixed] $value
* @param [array] $params
* @return [bool]
*/
public function validateMin($field, $value, $params)
type MyObj = object of RootObj
method output(self: ref MyObj) =
echo "Output something"
type ChildObj = object of MyObj
customvar: string
method newOutput(self: ref ChildObj) =
self.customvar = "Child output custom var too"
type MyObj = ref object of RootObj
method output(self: ref MyObj) =
echo "Output something"
type ChildObj = ref object of MyObj
customvar: string
method newOutput(self: ref ChildObj) =
self.customvar = "Child output custom var too"
@G4MR
G4MR / gist:785c7ad4c8cd03f45771
Created March 3, 2015 22:26
nim/regular expressions
import re
let foundyou = "foobar"
let foostart = re("^foo", {reIgnoreCase, reExtended, reMultiLine})
let barstart = re("^bar", {reIgnoreCase, reExtended, reMultiLine})
if foundyou.find(foostart) != -1:
echo "found foo at start!"
if foundyou.find(barstart) != -1:
Hint: unicode [Processing]
c:\users\lamonte\.nimble\pkgs\nre-0.6.0\nre.nim(371, 5) Hint: 'nre.renderBounds(str: string, bounds: Slice[system.int])' is declared but not
used [XDeclaredButNotUsed]
soundcloud.nim(9, 28) Info: template/generic instantiation from here
c:\users\lamonte\.nimble\pkgs\optional_t-1.2.0\optional_t.nim(67, 48) Info: template/generic instantiation from here
d:\nim\lib\system.nim(1993, 7) Error: undeclared identifier: 'pcreMatchBounds'
----------
import nre
@G4MR
G4MR / gist:8bec3d4c21c1ca0f4728
Created March 4, 2015 02:48
nim lang - nre (regular expression iteration)
var matches: seq[string] = @[]
for match in soundcloud_html.findIter(re("<meta itemprop=\"customitem\" content=\"([^\"]+)\" />", "im")):
matches.add(match.captures[0])
@G4MR
G4MR / gist:42d52305fabc0c5619ec
Created March 4, 2015 16:30
Nim - Html Entities to UTF8-16 Hex - Replace htmlparser.entityToUtf8
import unicode
proc entToUtf8(str: var string): string =
let Entities = @[
("nbsp", 0x00A0), ("iexcl", 0x00A1), ("cent", 0x00A2), ("pound", 0x00A3),
("curren", 0x00A4), ("yen", 0x00A5), ("brvbar", 0x00A6), ("sect", 0x00A7),
("uml", 0x00A8), ("copy", 0x00A9), ("ordf", 0x00AA), ("laquo", 0x00AB),
("not", 0x00AC), ("shy", 0x00AD), ("reg", 0x00AE), ("macr", 0x00AF),
("deg", 0x00B0), ("plusmn", 0x00B1), ("sup2", 0x00B2), ("sup3", 0x00B3),
("acute", 0x00B4), ("micro", 0x00B5), ("para", 0x00B6), ("middot", 0x00B7),
method getSurface*(self: Font; text_type, render_type: string): SurfacePtr =
case render_type:
of "utf8":
case text_type:
of "shade":
ttf.renderUtf8Shaded(self.font_ptr, self.text, self.color, self.bg_color)
of "solid":
ttf.renderUtf8Solid(self.font_ptr, self.text, self.color)
of "blendwrap":
ttf.renderUtf8BlendedWrapped(self.font_ptr, self.text, self.color, self.wrap_length)
import sdl2, sdl2.image as img
import helpers
import typetraits
type Image* = ref object
rw*: RWopsPtr
format: string
loaded: bool
surface: SurfacePtr
import sdl2, sdl2.image as img
import helpers
import typetraits
type Image* = ref object
rw*: RWopsPtr
format: string
loaded: bool
surface: SurfacePtr