shithub: hell

ref: f6808341cc21e3b4c27b6b69ae4a7a1e79fc92b5
dir: /references.go/

View raw version
package main

import (
	"strings"

	"github.com/mattn/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]interface{}, obj interface{}, index string) {
	debugindex := "!" + index
	(debugMap)[debugindex] = obj
}

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)
}