Rosetta

2023-02-20

Here are some code samples coming from the Rosetta project, a website that translate simple algorithms in different languages.

C

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_BUF 50

// Print the current date in formats DD-MM-YYYY and "day, month date, year".
int main(void)
{
    char buf[MAX_BUF];
    time_t seconds = time(NULL);
    struct tm *now = localtime(&seconds);
    (void) printf("%d-%d-%d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
    /* using the strftime (the result depends on the locale) */
    (void) strftime(buf, MAX_BUF, "%A, %B %e, %Y", now);
    (void) printf("%s\n", buf);
    return EXIT_SUCCESS;
}

lua

-- A comment to showcase comments highlighting.
print( os.date( "%Y-%m-%d" ) )
print( os.date( "%A, %B %d, %Y" ) )

rust

use chrono;

/// Print the current date in formats DD-MM-YYYY and "day, month date, year".
fn main() {
    let mut s = String::new();
    let now = chrono::Utc::now();
    s = now.format("%Y-%m-%d");
    println!("{}", s);
    s = now.format("%A, %B %d, %Y");
    println!("{}", s);
}

sh

echo "Using a builtin function to showcase its highlighting."
date -I
# Cut away the fields after the 4th space-delimited string.
date -R | sed 's/[^ ]* //4g'

vimscript

"See `:help strftime` - this function uses the C strftime() arguments' format.

echo strftime("%Y-%m-%d")
echo strftime("%A, %B %d, %Y")

container formats

Markdown

# A big title
some text

> A quotation of someone anonymous.

cfg

[Category]
Variable=value
OtherVariable="escaped string"
LastVariable=100

TOML

[aspect]
consideration = false

XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dict>
    <key>author</key>
    <string>la Fleur</string>
    <key>name</key>
    <string>some</string>
</dict>

SVG

<svg
        xmlns="http://www.w3.org/2000/svg"
        class="video-button"
        viewBox="0 0 18 24"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        fill="transparent"
        stroke="white"
>
    <rect fill="black" stroke="black" width="18" height="24" />
    <polygon points="5 3 18 12 5 21 5 3"></polygon>
    <text x="0" y="0">
        <tspan dy=0>
            Play !
        </tspan>
    </text>
</svg>

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Sample page</title>
    <!-- A comment. -->
    <script type="application/javascript">
        // A javascript comment.
        let sample = "A sample string";
        console.log(sample);
    </script>
    <style type="text/css">
        p {
            text-align: center;
        }
    </style>
</head>
<body>
    <p>
        Some text in the body.
    </p>
</body>
</html>