CodeSampleX

サンプル

golang.org/x/text v0.42.0: encoding.Decoder

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

sha256:3ec017571660c661fdf356f75e6969b1eef594ffbefc45605ca1c09f96cab0e0

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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-09-15

ケース

HOW
ゴール
verify golang.org/x/text/encoding.Decoder in pkg:golang/golang.org/x/text@v0.42.0
パッケージ
シンボル
  • golang.org/x/text/encoding.Decoder
作成日
2026-09-15T20:55:38Z

コントラクト

  1. Decoder.Bytes converts encoded byte slice to valid UTF-8 byte slice
  2. Decoder.String converts encoded string to valid UTF-8 string
  3. Decoder.Reader wraps io.Reader to decode streaming bytes into UTF-8
  4. Decoder.Transform satisfies transform.Transformer interface and converts bytes buffer
  5. Decoder.Transform returns transform.ErrShortDst when destination buffer is too small
  6. Decoder.Reset resets transformer state without error
  7. Nop NewDecoder produces identity Decoder that passes through bytes unchanged
  8. Replacement NewDecoder produces Decoder that decodes bytes to Unicode replacement character

ファイル

  • 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.Decoder in pkg:golang/golang.org/x/text@v0.42.0
Kind: HOW

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

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:0b95af8ebb2009243188948889c3f0ec7f1301bf9adb2be7dcb5ec0fba3bdbcc","contract":["Decoder.Bytes converts encoded byte slice to valid UTF-8 byte slice","Decoder.String converts encoded string to valid UTF-8 string","Decoder.Reader wraps io.Reader to decode streaming bytes into UTF-8","Decoder.Transform satisfies transform.Transformer interface and converts bytes buffer","Decoder.Transform returns transform.ErrShortDst when destination buffer is too small","Decoder.Reset resets transformer state without error","Nop NewDecoder produces identity Decoder that passes through bytes unchanged","Replacement NewDecoder produces Decoder that decodes bytes to Unicode replacement character"],"goal":"verify golang.org/x/text/encoding.Decoder in pkg:golang/golang.org/x/text@v0.42.0","kind":"HOW","packages":["pkg:golang/golang.org/x/text@v0.42.0"],"schemaVersion":1,"symbols":["golang.org/x/text/encoding.Decoder"]},"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.42.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/text@v0.42.0","symbols":["golang.org/x/text/encoding.Decoder"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.0

require golang.org/x/text v0.42.0
go.sum
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"log"

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

func main() {
	// Demonstrate golang.org/x/text/encoding.Decoder with Windows-1252 encoding
	var dec *encoding.Decoder = charmap.Windows1252.NewDecoder()

	// 1. Decode byte slice
	win1252Bytes := []byte{0x43, 0x61, 0x66, 0xe9, 0x20, 0x80} // "Café €" in Windows-1252
	utf8Bytes, err := dec.Bytes(win1252Bytes)
	if err != nil {
		log.Fatalf("failed to decode bytes: %v", err)
	}
	fmt.Printf("Decoded bytes: %s\n", string(utf8Bytes))

	// 2. Decode string
	utf8Str, err := dec.String(string(win1252Bytes))
	if err != nil {
		log.Fatalf("failed to decode string: %v", err)
	}
	fmt.Printf("Decoded string: %s\n", utf8Str)

	// 3. Decode streaming reader
	reader := dec.Reader(bytes.NewReader(win1252Bytes))
	streamOutput, err := io.ReadAll(reader)
	if err != nil {
		log.Fatalf("failed to decode reader: %v", err)
	}
	fmt.Printf("Decoded stream: %s\n", string(streamOutput))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/text/encoding.Decoder in pkg:golang/golang.org/x/text@v0.42.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/text@v0.42.0"
  ],
  "symbols": [
    "golang.org/x/text/encoding.Decoder"
  ]
}
test/contract.go
package main

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

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

func main() {
	dec := charmap.Windows1252.NewDecoder()

	// 1. Decoder.Bytes converts encoded byte slice to valid UTF-8 byte slice
	// In Windows-1252: 0x80 is Euro sign (€), 0xe9 is é
	inputBytes := []byte{0x43, 0x61, 0x66, 0xe9, 0x20, 0x80}
	decodedBytes, err := dec.Bytes(inputBytes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: error decoding bytes: %v\n", err)
		os.Exit(1)
	}
	if string(decodedBytes) != "Café €" {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: expected %q, got %q\n", "Café €", string(decodedBytes))
		os.Exit(1)
	}

	// 2. Decoder.String converts encoded string to valid UTF-8 string
	inputStr := string(inputBytes)
	decodedStr, err := dec.String(inputStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: error decoding string: %v\n", err)
		os.Exit(1)
	}
	if decodedStr != "Café €" {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected %q, got %q\n", "Café €", decodedStr)
		os.Exit(1)
	}

	// 3. Decoder.Reader wraps io.Reader to decode streaming bytes into UTF-8
	streamReader := dec.Reader(bytes.NewReader(inputBytes))
	streamResult, err := io.ReadAll(streamReader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: error reading from decoded reader: %v\n", err)
		os.Exit(1)
	}
	if string(streamResult) != "Café €" {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: expected %q, got %q\n", "Café €", string(streamResult))
		os.Exit(1)
	}

	// 4. Decoder.Transform satisfies transform.Transformer interface and converts bytes buffer
	var _ transform.Transformer = dec
	dst := make([]byte, 32)
	nDst, nSrc, err := dec.Transform(dst, []byte{0x80}, true)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: transform returned unexpected error: %v\n", err)
		os.Exit(1)
	}
	if nSrc != 1 || nDst != 3 || string(dst[:nDst]) != "€" {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: unexpected transform output: nSrc=%d, nDst=%d, out=%q\n", nSrc, nDst, string(dst[:nDst]))
		os.Exit(1)
	}

	// 5. Decoder.Transform returns transform.ErrShortDst when destination buffer is too small
	shortDst := make([]byte, 1)
	_, _, err = dec.Transform(shortDst, []byte{0x80}, true)
	if !errors.Is(err, transform.ErrShortDst) {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: expected ErrShortDst, got %v\n", err)
		os.Exit(1)
	}

	// 6. Decoder.Reset resets transformer state without error
	dec.Reset()

	// 7. Nop NewDecoder produces identity Decoder that passes through bytes unchanged
	nopDec := encoding.Nop.NewDecoder()
	nopInput := []byte("plain text sample")
	nopOutput, err := nopDec.Bytes(nopInput)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 7 failed: error on Nop Decoder: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(nopOutput, nopInput) {
		fmt.Fprintf(os.Stderr, "assertion 7 failed: expected unchanged bytes\n")
		os.Exit(1)
	}

	// 8. Replacement NewDecoder produces Decoder that decodes bytes to Unicode replacement character
	repDec := encoding.Replacement.NewDecoder()
	repOutput, err := repDec.Bytes([]byte("test input"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 8 failed: error on Replacement Decoder: %v\n", err)
		os.Exit(1)
	}
	if string(repOutput) != "\ufffd" {
		fmt.Fprintf(os.Stderr, "assertion 8 failed: expected replacement rune, got %q\n", string(repOutput))
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed.")
}

オリジンシーダー

匿名