Skip to content

Instantly share code, notes, and snippets.

create function imul(@a bigint, @b bigint) returns table as return
with
_0 as (select a = convert(binary(8), @a), b = convert(binary(8), @b)),
_1 as (select
hi_a = convert(bigint, substring(a, 1, 4)), lo_a = @a & 0xFFFFFFFF,
hi_b = convert(bigint, substring(b, 1, 4)), lo_b = @b & 0xFFFFFFFF from _0),
_2 as (select *, x = convert(binary(8), lo_a * lo_b) from _1),
_3 as (select s0 = substring(x, 5, 4), x = convert(bigint, substring(x, 1, 4)), hi_a, hi_b, lo_a, lo_b from _2),
_4 as (select x = (hi_a * lo_b + x) & 0xFFFFFFFF, s0, hi_b, lo_a from _3)
@gfody
gfody / Trie.cs
Created November 15, 2020 03:23
Trie<T>
public class Trie<T> : Dictionary<T, Trie<T>>
{
public KeyValuePair<T, Trie<T>> Parent;
public bool ContainsParts(IEnumerable<T> parts, out int index)
{
index = 0;
Trie<T> t = this, n = null;
foreach (var p in parts)
{
@gfody
gfody / stations.ps1
Created January 29, 2017 03:29
Import Digitally Imported and SomaFM radio stations into MusicBee
$doc = [xml]'<opml version="2.0"><body/></opml>'
$body = $doc.selectSingleNode('//body')
# somafm.com channels..
foreach($ch in (irm http://somafm.com/channels.xml).selectNodes('//channels/*')) {
$x = $doc.createElement('outline')
$x.setAttribute('text', "SomaFM - $($ch.title.innerText)")
$x.setAttribute('description', $ch.description.innerText)
$x.setAttribute('category', "$($ch.genre)/AAC/128k/")
$x.setAttribute('type', 'link')
@gfody
gfody / JsonToXml.cs
Last active July 19, 2019 17:29
converts json string to XmlElement, no System.Runtime.Serialization dependency
XmlElement JsonToXml(string json, string root = "root", string unnamed_array_item = "item")
{
var r = new Regex(@"\s*([\[\]{}]|""([^""]*)""\s*:|(true|false|null|-?\d+(?:\.\d+)?|""[^""]*"")\s*),?", RegexOptions.Multiline | RegexOptions.Compiled);
var doc = new XmlDocument(); var n = doc.CreateElement(root); var s = new Stack<XmlElement>(); string name = null, value = "";
var m = r.Match(json);
while (m.Success)
{
if (m.Groups[1].Value == "]") name = null;
else if (m.Groups[1].Value == "[") name = name ?? unnamed_array_item;
else if (m.Groups[1].Value.EndsWith(":")) name = m.Groups[2].Value;
@gfody
gfody / csv.sql
Last active March 7, 2023 21:47
handy table-valued functions for sql server
-- returns (row, col, value) table for specified csv, rows and cols are 1-based
create function string_split_csv(@string varchar(max), @field_separator char(1), @row_separator char(1)) returns table as
return
select a.row, b.col, b.s value
from (select row_number() over (order by (select 1)) row, * from string_split(@string, @row_separator)) a
cross apply (select row_number() over (order by (select 1)), iif(left(value, 1) in (char(10), char(13)), substring(value, 2, len(value) - 1), value)
from string_split(a.value, @field_separator)) b(col, s)
create function string_split_csv_quoted(@string varchar(max), @field_separator char(1), @row_separator char(1), @text_qualifier char(1)) returns table as
return