CodeSampleX

サンプル

golang.org/x/text v0.31.0: encoding.ReplaceUnsupported

検証済みサンプル — golang golang.org/x/text v0.31.0: encoding.ReplaceUnsupported. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:c76d82bf7e643ea82834eec25113826820728dae40cbe78fae5c657d6685bef8

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-11

ケース

HOW
ゴール
verify golang.org/x/text/encoding.ReplaceUnsupported in pkg:golang/golang.org/x/text@v0.31.0
パッケージ
シンボル
  • golang.org/x/text/encoding.ReplaceUnsupported
作成日
2026-10-11T17:12:34Z

コントラクト

  1. encoding.ReplaceUnsupported wraps an Encoder replacing unsupported runes with the destination replacement byte
  2. encoding.ReplaceUnsupported preserves valid runes in destination repertoire without replacement
  3. encoding.ReplaceUnsupported replaces multiple consecutive unsupported runes with corresponding replacement bytes
  4. encoding.ReplaceUnsupported returns transform.ErrShortDst when destination buffer is too small to receive replacement
  5. encoding.ReplaceUnsupported works with Encoder.String and Encoder.Bytes methods without returning repertoire errors

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify golang.org/x/text/encoding.ReplaceUnsupported in pkg:golang/golang.org/x/text@v0.31.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/text@v0.31.0
Demonstrate these symbols/APIs:
  - golang.org/x/text/encoding.ReplaceUnsupported

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:6ef559d31ad78023893636ea0200f617daceaadb128d30f092f4e8e6e2393140","contract":["encoding.ReplaceUnsupported wraps an Encoder replacing unsupported runes with the destination replacement byte","encoding.ReplaceUnsupported preserves valid runes in destination repertoire without replacement","encoding.ReplaceUnsupported replaces multiple consecutive unsupported runes with corresponding replacement bytes","encoding.ReplaceUnsupported returns transform.ErrShortDst when destination buffer is too small to receive replacement","encoding.ReplaceUnsupported works with Encoder.String and Encoder.Bytes methods without returning repertoire errors"],"goal":"verify golang.org/x/text/encoding.ReplaceUnsupported in pkg:golang/golang.org/x/text@v0.31.0","kind":"HOW","packages":["pkg:golang/golang.org/x/text@v0.31.0"],"schemaVersion":1,"symbols":["golang.org/x/text/encoding.ReplaceUnsupported"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/text@v0.31.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/text@v0.31.0","symbols":["golang.org/x/text/encoding.ReplaceUnsupported"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.24.0

require golang.org/x/text v0.31.0
go.sum
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
main.go
package main

import (
	"fmt"
	"os"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
)

func main() {
	baseEncoder := charmap.Windows1252.NewEncoder()
	wrappedEncoder := encoding.ReplaceUnsupported(baseEncoder)

	inputWithUnsupported := "Hello World \u4e16\u754c"
	encodedBytes, err := wrappedEncoder.Bytes([]byte(inputWithUnsupported))
	if err != nil {
		fmt.Fprintf(os.Stderr, "encoding failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Encoded bytes (with replacements): %q\n", encodedBytes)

	inputSupported := "Caf\u00e9"
	encodedSupported, err := wrappedEncoder.String(inputSupported)
	if err != nil {
		fmt.Fprintf(os.Stderr, "string encoding failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Encoded supported string (raw bytes): %x\n", encodedSupported)

	fmt.Println("golang.org/x/text/encoding.ReplaceUnsupported demonstrated successfully.")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/text/encoding.ReplaceUnsupported in pkg:golang/golang.org/x/text@v0.31.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/text@v0.31.0"
  ],
  "symbols": [
    "golang.org/x/text/encoding.ReplaceUnsupported"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"os"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
	"golang.org/x/text/transform"
)

func main() {
	if err := runContractTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract assertions passed successfully.")
}

func runContractTests() error {
	// Assertion 1: encoding.ReplaceUnsupported wraps an Encoder replacing unsupported runes with the destination replacement byte
	{
		baseEncoder := charmap.Windows1252.NewEncoder()
		_, rawErr := baseEncoder.Bytes([]byte("hello \uac00 world"))
		if rawErr == nil {
			return errors.New("assertion 1 failed: unwrapped base encoder unexpectedly succeeded on unsupported rune")
		}

		wrapped := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
		out, err := wrapped.Bytes([]byte("hello \uac00 world"))
		if err != nil {
			return fmt.Errorf("assertion 1 failed: wrapped encoder returned error: %w", err)
		}
		expected := []byte("hello \x1a world")
		if !bytes.Equal(out, expected) {
			return fmt.Errorf("assertion 1 failed: expected %q, got %q", expected, out)
		}
	}

	// Assertion 2: encoding.ReplaceUnsupported preserves valid runes in destination repertoire without replacement
	{
		wrapped := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
		input := "Caf\u00e9 123!"
		out, err := wrapped.Bytes([]byte(input))
		if err != nil {
			return fmt.Errorf("assertion 2 failed: wrapped encoder returned error: %w", err)
		}
		expected := []byte{'C', 'a', 'f', 0xe9, ' ', '1', '2', '3', '!'}
		if !bytes.Equal(out, expected) {
			return fmt.Errorf("assertion 2 failed: expected %v, got %v", expected, out)
		}
	}

	// Assertion 3: encoding.ReplaceUnsupported replaces multiple consecutive unsupported runes with corresponding replacement bytes
	{
		wrapped := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
		input := "\uac00\ub098\ub2e4"
		out, err := wrapped.Bytes([]byte(input))
		if err != nil {
			return fmt.Errorf("assertion 3 failed: wrapped encoder returned error: %w", err)
		}
		expected := []byte{0x1a, 0x1a, 0x1a}
		if !bytes.Equal(out, expected) {
			return fmt.Errorf("assertion 3 failed: expected %v, got %v", expected, out)
		}
	}

	// Assertion 4: encoding.ReplaceUnsupported returns transform.ErrShortDst when destination buffer is too small to receive replacement
	{
		wrapped := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
		src := []byte("\uac00")
		dst := make([]byte, 0)
		_, _, err := wrapped.Transform(dst, src, true)
		if !errors.Is(err, transform.ErrShortDst) {
			return fmt.Errorf("assertion 4 failed: expected transform.ErrShortDst, got %v", err)
		}
	}

	// Assertion 5: encoding.ReplaceUnsupported works with Encoder.String and Encoder.Bytes methods without returning repertoire errors
	{
		wrapped := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
		inputStr := "Value: \u20ac and \u4e16" // \u20ac is supported in Windows-1252 (0x80), \u4e16 is not
		outStr, err := wrapped.String(inputStr)
		if err != nil {
			return fmt.Errorf("assertion 5 failed: String returned error: %w", err)
		}
		expectedStr := "Value: \x80 and \x1a"
		if outStr != expectedStr {
			return fmt.Errorf("assertion 5 failed: expected %q, got %q", expectedStr, outStr)
		}

		outBytes, err := wrapped.Bytes([]byte(inputStr))
		if err != nil {
			return fmt.Errorf("assertion 5 failed: Bytes returned error: %w", err)
		}
		if !bytes.Equal(outBytes, []byte(expectedStr)) {
			return fmt.Errorf("assertion 5 failed: expected bytes %q, got %q", expectedStr, outBytes)
		}
	}

	return nil
}

オリジンシーダー

匿名