r/tinycode • • Mar 03 '19

filet v0.1.0 released! A fully blown file manager in 679 LOC.

Thumbnail
github.com
26 Upvotes

r/tinycode • • Feb 23 '19

C# globbing/wildcard function (simplified Regex) to find and replace any text with ★ and ✪ as special wildcard characters, featuring efficiency, case (in)sensitivity, and multiple-query on the trot

6 Upvotes

Took me quite a few hours to create this, and it's fairly optimized. I hope someone finds it useful! Pretty easy to convert into C or C++ since barely any high level functions are used.

"But why not just use Regex.Replace()?" you may ask. Well, if you have created your own string class (e.g: to allow strings larger than 2 billion chars, or to allow fast prepending instead of just StringBuilder's appending, or even just to allow fast in place character replacements without creating a new string), then you'll find that Regex won't work with your brand new class, and so you'll need to reinvent the wheel. Well, you would except I've done it now :)

Example usage:

  • wildcard("Hello and welcome", "hello✪w★l", "be") results in "become".
  • wildcard("Hello and welcome", new string[] {"hell","and","wel"}, new string[] { "Jell","can","over" }) results in "Jello can overcome".

I was hoping one of you may offer advice to improve the efficiency to make it faster. For one test I did, I found it was around twice as slow as the Regex equivalent, but perhaps increasing efficiency would make the code a lot larger!

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////// Search for a string/s inside 'text' using the 'find' parameter, and replace with a string/s using the replace parameter
// ✪ represents multiple wildcard characters (non-greedy)
// ★ represents a single wildcard character
public string wildcard(string text, string find, string replace, bool caseSensitive = false)
{
	return wildcard(text, new string[] { find }, new string[] { replace }, caseSensitive);
}
public string wildcard(string text, string[] find, string[] replace, bool caseSensitive = false)
{
	int textLength = text.Length;
	if (textLength == 0) return text;			// Degenerate case
	StringBuilder sb = new StringBuilder();		// The new adjusted string with replacements
	for (int i = 0; i < textLength; i++)		// Go through every letter of the original large text
	{
		bool foundMatch = false;				// Assume match hasn't been found to begin with
		for(int query=0; query < find.Length; query++) {		// Go through each query in turn
			int findLength = find[query].Length;
			if (findLength == 0) continue;		// Ignore empty queries
			int f = 0;	int g = 0;				// Query cursor and text cursor
			bool multiWild = false;				// multiWild is ✪ symbol which represents many wildcard characters
			int multiWildPosition = 0;			
		
			while(true)	{						// Loop through query characters
				if ((f != 0 || g != 0) && (f == findLength || (i + g) == textLength)) break;		// Bounds checking
				char cf = find[query][f];											// Character in the query (f is the offset)
				char cg = text[i + g];												// Character in the text (g is the offset)
				if (!caseSensitive) cg = char.ToLowerInvariant(cg);
				if (!multiWild && cg != cf && cf != '★' && cf != '✪') break;		// Break search, and thus no match is found
				if (cf == '✪') { multiWild = true; multiWildPosition = f; f++; continue; }				// Multi-char wildcard activated. Move query cursor, and reloop
				if (multiWild && cg != cf && cf != '★') { f = multiWildPosition + 1; g++; continue; }		// Match since MultiWild has failed, so return query cursor to MultiWild position
				f++; g++;															// Reaching here means that a single character was matched, so move both query and text cursor along one
			}

			if (f == findLength)
			{			// If true, query cursor has reached the end of the query, so a match has been found!!!
				sb.Append(replace[query]);			// Append replacement
				foundMatch = true;
				if (find[query][f - 1] == '✪') { i = textLength; break; }		// if The MultiWild is the last char in the query, then the rest of the string is a match, and close off
				i += g - 1;														// Move text cursor along by the amount equivalent to its found match
				break;
			}
		}
		if (!foundMatch) sb.Append(text[i]);	// If a match wasn't found at that point in the text, then just append the original character
	}
	return sb.ToString();
}

r/tinycode • • Feb 19 '19

nnn - popular file manager in 55K, v2.3 released with many new features!

Thumbnail
github.com
19 Upvotes

r/tinycode • • Feb 15 '19

Jing: A tiny static site generator packing a punch

Thumbnail
github.com
17 Upvotes

r/tinycode • • Feb 13 '19

A bash one liner to make scripts out of bash one liners

20 Upvotes

You've written and executed the perfect one liner. Now you want to make a script out of it. Nano? Vim? No! One liner that one liner. Don't retype it.

Note the dual use of string delimiters ' and ", and the lack of space between them. I had my reasons. Meddle not with extraneous spaces. I used printf instead of echo because.

printf '#!/usr/bin/env bash\n'"!!\n" > world_peace_achieved.sh

Suggestions for improvements gladly welcomed.


r/tinycode • • Jan 31 '19

realtime fourier of 90000 pairs of frequency and decay of mouseY movements without even using GPU (12.5kB executable jar file containing source)

Post image
25 Upvotes

r/tinycode • • Jan 28 '19

KABOOM! in 180 lines of bare C++

Thumbnail
github.com
30 Upvotes

r/tinycode • • Jan 22 '19

Tile map editor in 70 lines of pure JavaScript

Thumbnail
slicker.me
22 Upvotes

r/tinycode • • Jan 19 '19

Pixel graphics in terminal with unicode braille characters

Thumbnail
github.com
10 Upvotes

r/tinycode • • Jan 03 '19

WonderCMS 2.6.0 - extra small, open source CMS (no database)

Thumbnail
self.wondercms
13 Upvotes

r/tinycode • • Jan 02 '19

fast and interactive tldr client written with go

Thumbnail
github.com
5 Upvotes

r/tinycode • • Dec 29 '18

crcx/build: A simple alternative to "make"

Thumbnail
github.com
8 Upvotes

r/tinycode • • Dec 25 '18

Tired of KTByte entries

25 Upvotes

Why is tinycode being overrun by ktbyte.com snippets now? I am getting a bit tired of seeing them now.


r/tinycode • • Dec 10 '18

Introducing /r/OpenSourceVSTi and A Contest For Making VST's Using AirWindows FOSS Code -- Developers & All Ideas Wanted!

0 Upvotes

r/tinycode • • Dec 05 '18

Minesweeper game in 100 lines of pure JavaScript

Thumbnail
slicker.me
26 Upvotes

r/tinycode • • Nov 23 '18

Sending emojis (or anything else) via sound using the command line.

4 Upvotes

r/tinycode • • Nov 18 '18

fff: fucking fast file-manager. Around 100 line of code in bash.

Thumbnail
github.com
53 Upvotes

r/tinycode • • Nov 10 '18

PigletC, a toy C-like language compiler for PigletVM (about 300 lines of Python code)

Thumbnail
github.com
22 Upvotes

r/tinycode • • Oct 25 '18

Realtime fractal animation in 32 lines of pure JavaScript

Thumbnail
slicker.me
25 Upvotes

r/tinycode • • Oct 17 '18

torque - A simple TUI for transmission-daemon (torrent daemon) written in pure bash (55~ LOC, vim-like scrolling).

Thumbnail
github.com
21 Upvotes

r/tinycode • • Oct 17 '18

TicTacToe in one line (Python3.5+)

Thumbnail
self.python
5 Upvotes

r/tinycode • • Oct 04 '18

Interactive text-to speach in 35 lines of C

Thumbnail
gitlab.com
0 Upvotes

r/tinycode • • Oct 01 '18

Micro Mages: How we fit an NES game into 40 Kilobytes

Thumbnail
youtube.com
42 Upvotes

r/tinycode • • Sep 27 '18

The Making of Underrun – A WebGL Shooter in 13kb of JavaScript

Thumbnail
phoboslab.org
25 Upvotes