<commit_before>package flux

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"github.com/pkg/errors"
)

const (
	dockerHubHost    = "index.docker.io"
	dockerHubLibrary = "library"
)

var (
	ErrInvalidImageID   = errors.New("invalid image ID")
	ErrBlankImageID     = errors.Wrap(ErrInvalidImageID, "blank image name")
	ErrMalformedImageID = errors.Wrap(ErrInvalidImageID, `expected image name as either <image>:<tag> or just <image>`)
)

// ImageID is a fully qualified name that refers to a particular Image.
// It is in the format: host[:port]/Namespace/Image[:tag]
// Here, we refer to the "name" == Namespace/Image
type ImageID struct {
	Host, Namespace, Image, Tag string
}

func ParseImageID(s string) (ImageID, error) {
	if s == "" {
		return ImageID{}, ErrBlankImageID
	}
	var img ImageID
	parts := strings.Split(s, ":")
	switch len(parts) {
	case 0:
		return ImageID{}, ErrMalformedImageID
	case 1:
		img.Tag = "latest"
	case 2:
		img.Tag = parts[1]
		s = parts[0]
	case 3: // There might be three parts if there is a host with a custom port
		img.Tag = parts[2]
		s = s[:strings.LastIndex(s, ":")]
	default:
		return ImageID{}, ErrMalformedImageID
	}
	if s == "" {
		return ImageID{}, ErrBlankImageID
	}
	parts = strings.Split(s, "/")
	switch len(parts) {
	case 1:
		img.Host = dockerHubHost
		img.Namespace = dockerHubLibrary
		img.Image = parts[0]
	case 2:
		img.Host = dockerHubHost
		img.Namespace = parts[0]
		img.Image = parts[1]
	case 3:
		img.Host = parts[0]
		img.Namespace = parts[1]
		img.Image = parts[2]
	default:
		return ImageID{}, ErrMalformedImageID
	}
	return img, nil<commit_msg>Replace docker.io with index.docker.io in ImageID<commit_after>package flux

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"github.com/pkg/errors"
)

const (
	dockerHubHost    = "index.docker.io"
	dockerHubLibrary = "library"

	oldDockerHubHost = "docker.io"
)

var (
	ErrInvalidImageID   = errors.New("invalid image ID")
	ErrBlankImageID     = errors.Wrap(ErrInvalidImageID, "blank image name")
	ErrMalformedImageID = errors.Wrap(ErrInvalidImageID, `expected image name as either <image>:<tag> or just <image>`)
)

// ImageID is a fully qualified name that refers to a particular Image.
// It is in the format: host[:port]/Namespace/Image[:tag]
// Here, we refer to the "name" == Namespace/Image
type ImageID struct {
	Host, Namespace, Image, Tag string
}

func ParseImageID(s string) (ImageID, error) {
	if s == "" {
		return ImageID{}, ErrBlankImageID
	}
	var img ImageID
	parts := strings.Split(s, ":")
	switch len(parts) {
	case 0:
		return ImageID{}, ErrMalformedImageID
	case 1:
		img.Tag = "latest"
	case 2:
		img.Tag = parts[1]
		s = parts[0]
	case 3: // There might be three parts if there is a host with a custom port
		img.Tag = parts[2]
		s = s[:strings.LastIndex(s, ":")]
	default:
		return ImageID{}, ErrMalformedImageID
	}
	if s == "" {
		return ImageID{}, ErrBlankImageID
	}
	parts = strings.Split(s, "/")
	switch len(parts) {
	case 1:
		img.Host = dockerHubHost
		img.Namespace = dockerHubLibrary
		img.Image = parts[0]
	case 2:
		img.Host = dockerHubHost
		img.Namespace = parts[0]
		img.Image = parts[1]
	case 3:
		// Replace docker.io with index.docker.io (#692)
		if parts[0] == oldDockerHubHost {
			parts[0] = dockerHubHost
		}
		img.Host = parts[0]
		img.Namespace = parts[1]
		img.Image = parts[2]
	default:
		return ImageID{}, ErrMalformedImageID
	}
	return img, nil
{'fraction_non_alphanumeric': 0.13063854715875806, 'fraction_numerical': 0.010837727006444054, 'mean_word_length': 3.7963483146067416, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3965083', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1218, 'n_words': 342}