Task: Text between


C++: #include <iostream>

std::ostream& operator<<(std::ostream& out, const std::string& str) {
    return out << str.c_str();
}

std::string textBetween(const std::string& source, const std::string& beg, const std::string& end) {
    size_t startIndex;
    if (beg == "start") {
        startIndex = 0;
    } else {
        startIndex = source.find(beg);
        if (startIndex == std::string::npos) {
            return "";
        }
        startIndex += beg.length();
    }

    size_t endIndex = source.find(end, startIndex);
    if (endIndex == std::string::npos || end == "end") {
        return source.substr(startIndex);
    }

    return source.substr(startIndex, endIndex - startIndex);
}

void print(const std::string& source, const std::string& beg, const std::string& end) {
    using namespace std;
    cout << "text: '" << source << "'\n";
    cout << "start: '" << beg << "'\n";
    cout << "end: '" << end << "'\n";
    cout << "result: '" << textBetween(source, beg, end) << "'\n";
    cout << '\n';
}

int main() {
    print("Hello Rosetta Code world", "Hello ", " world");
    print("Hello Rosetta Code world", "start", " world");
    print("Hello Rosetta Code world", "Hello ", "end");
    print("<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">", "<text>", "<table>");
    print("<table style=\"myTable\"><tr><td>hello world</td></tr></table>", "<table>", "</table>");
    print("The quick brown fox jumps over the lazy other fox", "quick ", " fox");
    print("One fish two fish red fish blue fish", "fish ", " red");
    print("FooBarBazFooBuxQuux", "Foo", "Foo");

    return 0;
}



Go: package main

import (
    "fmt"
    "strings"
)

func textBetween(str, start, end string) string {
    if str == "" || start == "" || end == "" {
        return str
    }
    s := 0
    if start != "start" {
        s = strings.Index(str, start)
    }
    if s == -1 {
        return ""
    }
    si := 0
    if start != "start" {
        si = s + len(start)
    }
    e := len(str)
    if end != "end" {
        e = strings.Index(str[si:], end)
        if e == -1 {
            return str[si:]
        }
        e += si
    }
    return str[si:e]
}

func main() {
    texts := [9]string{
        "Hello Rosetta Code world",
        "Hello Rosetta Code world",
        "Hello Rosetta Code world",
        "</div><div style=\"chinese\">你好嗎</div>",
        "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">",
        "<table style=\"myTable\"><tr><td>hello world</td></tr></table>",
        "The quick brown fox jumps over the lazy other fox",
        "One fish two fish red fish blue fish",
        "FooBarBazFooBuxQuux",
    }
    starts:= [9]string{
        "Hello ", "start", "Hello ", "<div style=\"chinese\">",
        "<text>", "<table>", "quick ", "fish ", "Foo",
    }
    ends := [9]string{
        " world", " world", "end", "</div>", "<table>",
        "</table>", " fox", " red", "Foo",
    }
    for i, text := range texts {
        fmt.Printf("Text: \"%s\"\n", text)
        fmt.Printf("Start delimiter: \"%s\"\n", starts[i])
        fmt.Printf("End delimiter: \"%s\"\n", ends[i])
        b := textBetween(text, starts[i], ends[i])
        fmt.Printf("Output: \"%s\"\n\n", b)
    }
}