codanna 0.9.19

Code Intelligence for Large Language Models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Helper utilities for common operations
//!
//! This package demonstrates:
//! - Utility function implementations
//! - Generic programming patterns (Go 1.18+)
//! - Common data processing patterns
//! - Interface definitions and implementations
//! - Error handling utilities

package utils

import (
	"fmt"
	"math"
	"strings"
	"sync"
	"time"
)

// Package-level constants (exported)
const (
	ModuleName     = "utils"
	Version        = "1.0.0"
	MaxBatchSize   = 1000
	DefaultRetries = 3
	BackoffBaseMS  = 100
)

// Package-level variables
var (
	// Exported package variables
	DefaultProcessor *DataProcessor
	// unexported package variables
	processorCount int
	globalMutex    sync.RWMutex
)

// FormatOutput formats a message for output display
func FormatOutput(message string) string {
	timestamp := time.Now().Unix()
	return fmt.Sprintf("[%d] OUTPUT: %s", timestamp, message)
}

// ValidateInput validates input data (basic email-like validation)
func ValidateInput(data string) bool {
	return len(data) >= 5 && strings.Contains(data, "@")
}

// ValidateInputDetailed provides detailed input validation with error reporting
func ValidateInputDetailed(data string) error {
	if data == "" {
		return NewValidationError(ErrEmpty, "input cannot be empty")
	}

	if len(data) < 5 {
		return NewValidationError(ErrTooShort, fmt.Sprintf("input too short (minimum 5 characters, got %d)", len(data)))
	}

	if len(data) > 254 {
		return NewValidationError(ErrTooLong, fmt.Sprintf("input too long (maximum 254 characters, got %d)", len(data)))
	}

	if !strings.Contains(data, "@") {
		return NewValidationError(ErrInvalidFormat, "input must contain @ symbol")
	}

	return nil
}

// DataProcessor handles data transformation with configurable behavior
type DataProcessor struct {
	config         map[string]string
	processedCount int
	mutex          sync.RWMutex
}

// NewDataProcessor creates a new data processor with configuration
func NewDataProcessor(config map[string]string) *DataProcessor {
	globalMutex.Lock()
	processorCount++
	globalMutex.Unlock()

	if config == nil {
		config = make(map[string]string)
	}

	return &DataProcessor{
		config:         config,
		processedCount: 0,
	}
}

// Process processes input data according to configuration
func (dp *DataProcessor) Process(data string) string {
	dp.mutex.Lock()
	dp.processedCount++
	count := dp.processedCount
	dp.mutex.Unlock()

	mode := dp.GetConfig("transform", "standard")

	switch mode {
	case "uppercase":
		return strings.ToUpper(data)
	case "lowercase":
		return strings.ToLower(data)
	case "reverse":
		return reverseString(data)
	case "trim":
		return strings.TrimSpace(data)
	case "capitalize":
		return capitalizeWords(data)
	default:
		return fmt.Sprintf("processed(%d): %s", count, data)
	}
}

// ProcessBatch processes multiple items efficiently
func (dp *DataProcessor) ProcessBatch(items []string) []string {
	results := make([]string, len(items))

	// Process items in chunks for better performance
	chunkSize := MaxBatchSize
	if len(items) < chunkSize {
		chunkSize = len(items)
	}

	for i := 0; i < len(items); i += chunkSize {
		end := i + chunkSize
		if end > len(items) {
			end = len(items)
		}

		for j := i; j < end; j++ {
			results[j] = dp.Process(items[j])
		}
	}

	return results
}

// Stats returns processing statistics
func (dp *DataProcessor) Stats() ProcessingStats {
	dp.mutex.RLock()
	defer dp.mutex.RUnlock()

	return ProcessingStats{
		ProcessedCount: dp.processedCount,
		ConfigCount:    len(dp.config),
		Efficiency:     dp.calculateEfficiency(),
	}
}

// SetConfig updates processor configuration
func (dp *DataProcessor) SetConfig(key, value string) {
	dp.mutex.Lock()
	defer dp.mutex.Unlock()
	dp.config[key] = value
}

// GetConfig retrieves configuration value with optional default
func (dp *DataProcessor) GetConfig(key, defaultValue string) string {
	dp.mutex.RLock()
	defer dp.mutex.RUnlock()

	if value, exists := dp.config[key]; exists {
		return value
	}
	return defaultValue
}

// Reset resets processor state
func (dp *DataProcessor) Reset() {
	dp.mutex.Lock()
	defer dp.mutex.Unlock()
	dp.processedCount = 0
}

// Clone creates a copy of the processor
func (dp *DataProcessor) Clone() *DataProcessor {
	dp.mutex.RLock()
	defer dp.mutex.RUnlock()

	configCopy := make(map[string]string)
	for k, v := range dp.config {
		configCopy[k] = v
	}

	return &DataProcessor{
		config:         configCopy,
		processedCount: 0, // Reset count for new instance
	}
}

// IsMode checks if processor is configured for a specific mode
func (dp *DataProcessor) IsMode(mode string) bool {
	return dp.GetConfig("transform", "standard") == mode
}

// Private methods

func (dp *DataProcessor) calculateEfficiency() float64 {
	if len(dp.config) == 0 {
		return 0.0
	}
	return float64(dp.processedCount) / float64(len(dp.config))
}

// ProcessingStats holds statistics about data processing
type ProcessingStats struct {
	ProcessedCount int     `json:"processed_count"`
	ConfigCount    int     `json:"config_count"`
	Efficiency     float64 `json:"efficiency"`
}

// String implements fmt.Stringer interface
func (ps ProcessingStats) String() string {
	return fmt.Sprintf("ProcessingStats(processed: %d, config_entries: %d, efficiency: %.2f)",
		ps.ProcessedCount, ps.ConfigCount, ps.Efficiency)
}

// Generic utility functions

// SafeDivision performs safe division returning nil for division by zero
func SafeDivision[T ~float32 | ~float64](a, b T) *T {
	if b == 0 {
		return nil
	}
	result := a / b
	return &result
}

// RetryWithBackoff executes an operation with exponential backoff
func RetryWithBackoff[T any](
	operation func() (T, error),
	maxAttempts int,
	baseDelayMS int,
) (T, error) {
	var zero T

	for attempt := 1; attempt <= maxAttempts; attempt++ {
		result, err := operation()
		if err == nil {
			return result, nil
		}

		if attempt == maxAttempts {
			return zero, fmt.Errorf("operation failed after %d attempts: %w", maxAttempts, err)
		}

		// Calculate delay with exponential backoff
		delay := time.Duration(baseDelayMS*int(math.Pow(2, float64(attempt-1)))) * time.Millisecond
		fmt.Printf("Retry attempt %d after %v delay\n", attempt, delay)
		time.Sleep(delay)
	}

	return zero, fmt.Errorf("unexpected end of retry loop")
}

// FindDuplicates finds duplicate items in a slice
func FindDuplicates[T comparable](items []T) []T {
	seen := make(map[T]bool)
	duplicates := make([]T, 0)

	for _, item := range items {
		if seen[item] {
			// Only add to duplicates once
			found := false
			for _, dup := range duplicates {
				if dup == item {
					found = true
					break
				}
			}
			if !found {
				duplicates = append(duplicates, item)
			}
		} else {
			seen[item] = true
		}
	}

	return duplicates
}

// MergeMaps merges two maps, with the second taking precedence
func MergeMaps[K comparable, V any](map1, map2 map[K]V) map[K]V {
	result := make(map[K]V)

	// Copy first map
	for k, v := range map1 {
		result[k] = v
	}

	// Override with second map
	for k, v := range map2 {
		result[k] = v
	}

	return result
}

// String utility functions

// TruncateString truncates a string to specified length with ellipsis
func TruncateString(s string, maxLength int) string {
	if len(s) <= maxLength {
		return s
	}

	if maxLength <= 3 {
		return s[:maxLength]
	}

	return s[:maxLength-3] + "..."
}

// NormalizeWhitespace normalizes whitespace in a string
func NormalizeWhitespace(s string) string {
	fields := strings.Fields(s)
	return strings.Join(fields, " ")
}

// ExtractDomainFromEmail extracts domain part from email address
func ExtractDomainFromEmail(email string) (string, error) {
	parts := strings.Split(email, "@")
	if len(parts) != 2 {
		return "", fmt.Errorf("invalid email format: %s", email)
	}
	return strings.ToLower(parts[1]), nil
}

// capitalizeWords capitalizes the first letter of each word
func capitalizeWords(s string) string {
	words := strings.Fields(s)
	for i, word := range words {
		if len(word) > 0 {
			words[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:])
		}
	}
	return strings.Join(words, " ")
}

// reverseString reverses a string
func reverseString(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

// Package-level utility functions

// GetModuleInfo returns information about this module
func GetModuleInfo() string {
	globalMutex.RLock()
	defer globalMutex.RUnlock()
	return fmt.Sprintf("%s v%s (processors created: %d)", ModuleName, Version, processorCount)
}

// CreateDefaultProcessor creates a data processor with default configuration
func CreateDefaultProcessor() *DataProcessor {
	config := map[string]string{
		"transform": "standard",
		"encoding":  "utf8",
		"mode":      "production",
	}
	return NewDataProcessor(config)
}

// Validation error types
type ValidationErrorCode int

const (
	ErrEmpty ValidationErrorCode = iota
	ErrTooShort
	ErrTooLong
	ErrInvalidFormat
	ErrInvalidCharacters
)

// ValidationError represents input validation errors
type ValidationError struct {
	Code    ValidationErrorCode
	Message string
	Field   string
}

func NewValidationError(code ValidationErrorCode, message string) *ValidationError {
	return &ValidationError{
		Code:    code,
		Message: message,
	}
}

func NewValidationFieldError(code ValidationErrorCode, message, field string) *ValidationError {
	return &ValidationError{
		Code:    code,
		Message: message,
		Field:   field,
	}
}

func (e *ValidationError) Error() string {
	if e.Field != "" {
		return fmt.Sprintf("validation error [%d] in field '%s': %s", int(e.Code), e.Field, e.Message)
	}
	return fmt.Sprintf("validation error [%d]: %s", int(e.Code), e.Message)
}

// Is implements error comparison
func (e *ValidationError) Is(target error) bool {
	if other, ok := target.(*ValidationError); ok {
		return e.Code == other.Code
	}
	return false
}

// Package initialization
func init() {
	fmt.Println("[INIT] Utils package initialized")

	// Initialize default processor
	DefaultProcessor = CreateDefaultProcessor()
}