shithub: hell

ref: 906489cfcc61b985c2300936ea22ec5fedc2f013
dir: /references.go/

View raw version
package main

import (
	"fmt"
	"strings"

	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
)

var charSequence = []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}

// charToIndex maps each character in charSequence to its index for quick lookup.
var charToIndex = make(map[rune]int)

func initReferenceSystem() {
	// Initialize the charToIndex map for efficient lookups.
	for i, char := range charSequence {
		charToIndex[char] = i
	}
}

func saveDebugRef(debugMap map[string]any, obj any, index string) {
	debugindex := "!" + index
	(debugMap)[debugindex] = obj
}

// just increment a postref given a status and struct
func justIncrementPostref(ref *postref, post *mastodon.Status) {
	IncrementRef(ref, post)
	ref.ref = IncrementString(ref.ref)
}

// print and increment a status given a postref struct
func (hc *Hellclient) printAndIncrement(ref *postref, post *mastodon.Status) (returnPost *mastodon.Status) {
	returnPost = hc.printPostS(*ref, post)
	IncrementRef(ref, post)
	ref.ref = IncrementString(ref.ref)
	return
}

func (hc *Hellclient) renderAndIncrement(ref *postref, post *mastodon.Status) string {
	plaintext := hc.renderPostS(*ref, post)
	IncrementRef(ref, post)
	ref.ref = IncrementString(ref.ref)
	return plaintext
}

func printAndIncrementDetailed(ref *postref, post *mastodon.Status, format func(*mastodon.Status, string) string) {
	fmt.Println(format(post, ref.ref))
	IncrementRef(ref, post)
	ref.ref = IncrementString(ref.ref)
}

// Saves a status when given a postref struct
func IncrementRef(ref *postref, status *mastodon.Status) {
	saveCustomStatusRef(ref.postmap, status, ref.ref, ref.prefix)
}

func saveWorkRef(statusMap map[string]*mastodon.Status, post *mastodon.Status, index string) {
	saveCustomStatusRef(statusMap, post, index, "?")
}

func saveCustomStatusRef(statusMap map[string]*mastodon.Status, post *mastodon.Status, index string, prefix string) {
	index = prefix + index
	(statusMap)[index] = post
}

func saveRef(statusMap map[string]*mastodon.Status, post *mastodon.Status, index string) {
	(statusMap)[index] = post
}

func IncrementSequence(r rune) (rune, bool) {
	idx, exists := charToIndex[r]
	if !exists {
		// If the character is not in our defined sequence, return it as is, no carry.
		return r, false
	}

	// Handle specific rules first
	if r == 'z' {
		return '0', false // z to 0
	}

	if r == '9' {
		return 'a', true
	}

	if idx < len(charSequence)-1 {
		return charSequence[idx+1], false
	}

	return ' ', false
}

func IncrementString(s string) string {
	runes := []rune(s)
	n := len(runes)
	carry := false

	for i := n - 1; i >= 0; i-- {
		currentRune := runes[i]
		nextRune, currentCarry := IncrementSequence(currentRune)

		runes[i] = nextRune
		carry = currentCarry

		if !carry {
			// No carry, so we're done with the increment
			return string(runes)
		}
		// If there's a carry, continue to the next character to the left
	}

	// If we've iterated through all characters and still have a carry,
	// it means we need to expand the string (e.g., "9" -> "aa", "z9" -> "0a", "99" -> "aa")
	if carry {
		var sb strings.Builder
		sb.WriteRune(charSequence[0]) // Prepend the first character of the sequence ('a')
		sb.WriteString(string(runes))
		return sb.String()
	}

	return string(runes)
}