I Split On "Your Grave"
Examples of polylingual grave splitting.
Splitting a string into multiple strings on a specific delimiter is a very common problem in modern software development. This website provides example code for doing so in a variety of programming languages.
In all of the examples, the delimiter is the string 'your grave'.
C
static char **
grow(char **result, size_t count, char *ptr) {
result = realloc(result, (count + 1) * sizeof(char *));
if (!result) {
fprintf(stderr, "Memory allocation error.\n");
exit(1);
}
result[count - 1] = ptr;
result[count] = NULL;
return result;
}
char **
isoyg(char *src) {
const char *PHRASE = "your grave";
const size_t PHRASE_LEN = strlen(PHRASE);
int count = 1;
char **result = grow(NULL, count, src);
for (char *ptr = src; *ptr; ptr++) {
if (strncmp(PHRASE, ptr, PHRASE_LEN) == 0) {
++count;
*ptr = '\0';
ptr += PHRASE_LEN;
result = grow(result, count, ptr);
}
}
return result;
}
(download)
Haskell
import System.Environment
import Data.List
import Data.List.Split -- Type 'cabal install split'
isoyg :: String -> [String]
isoyg s = endBy "your grave" s
splitAll :: [String] -> [[String]]
splitAll args = map isoyg args
splitAndJoin :: [String] -> String
splitAndJoin args =
let merge lns = foldr (\i r -> i ++ "\n" ++ r) "" lns
args2 = map merge $ splitAll args
in merge args2
main = do
args <- getArgs
putStr $ splitAndJoin args
(download)
Perl
#!/usr/bin/env perl
use strict;
use warnings;
print join("\n", split(/your grave/)), "\n\n" foreach (@ARGV)
(download)
Python
#!/usr/bin/env python
import sys
def isoyg(s): return s.split("your grave")
for s in sys.argv[1:]:
print "\n".join(isoyg(s)), "\n"
(download)
Ruby
#!/usr/bin/env ruby
ARGV.each{|arg| puts arg.split(/your grave/).join("\n") + "\n\n" }
(download)