CodeSampleX

示例

golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa: utf8string.NewString, utf8string.String.At, utf8string.String.Slice

已验证示例 — golang golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa: utf8string.NewString, utf8string.String.At, utf8string.String.Slice. contract 在 go 1.26 …

sha256:5e77481322515a3ad1d270bbfd7c424701cfb56e343b326b849484a09f9ce8f7

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go 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-03

案例

HOW
目标
verify pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
包
符号
  • golang.org/x/exp/utf8string.NewString
  • golang.org/x/exp/utf8string.String.At
  • golang.org/x/exp/utf8string.String.Slice
创建时间
2026-09-03T23:38:00Z

契约

  1. utf8string.NewString initializes String and reports RuneCount, IsASCII, and original string value
  2. utf8string.At indexes multi-byte runes by character position without splitting multi-byte sequences
  3. utf8string.Slice extracts string substrings using rune boundaries rather than byte offsets
  4. utf8string.String.Init initializes an existing struct instance and supports rune counting and slicing

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go
  • utf8helper.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 pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
Demonstrate these symbols/APIs:
  - golang.org/x/exp/utf8string.NewString
  - golang.org/x/exp/utf8string.String.At
  - golang.org/x/exp/utf8string.String.Slice

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:d3cb372c5cd74955927ac946d422178448a6c96b1c6eb196b291c91edcce78a2","contract":["utf8string.NewString initializes String and reports RuneCount, IsASCII, and original string value","utf8string.At indexes multi-byte runes by character position without splitting multi-byte sequences","utf8string.Slice extracts string substrings using rune boundaries rather than byte offsets","utf8string.String.Init initializes an existing struct instance and supports rune counting and slicing"],"goal":"verify pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa","kind":"HOW","packages":["pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"],"schemaVersion":1,"symbols":["golang.org/x/exp/utf8string.NewString","golang.org/x/exp/utf8string.String.At","golang.org/x/exp/utf8string.String.Slice"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa","symbols":["golang.org/x/exp/utf8string.NewString","golang.org/x/exp/utf8string.String.At","golang.org/x/exp/utf8string.String.Slice"],"verifierAdapter":"golang@1"}
go.mod
module example.com/utf8sample

go 1.26.0

require golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa
go.sum
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa h1:QSyA8ishJCyT21kER9KwNt0b7BM3iRK4x9QXhjN5Fdk=
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa/go.mod h1:zeBbvyFKDaLwa7CH/zI8KXt7gTl14SF7sO08Pl5jBCM=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"
  ],
  "symbols": [
    "golang.org/x/exp/utf8string.NewString",
    "golang.org/x/exp/utf8string.String.At",
    "golang.org/x/exp/utf8string.String.Slice"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	utf8sample "example.com/utf8sample"
	"golang.org/x/exp/utf8string"
)

func main() {
	// Assertion 1: utf8string.NewString correctly computes RuneCount and IsASCII
	asciiStr := "Hello, World!"
	uAscii := utf8string.NewString(asciiStr)
	if uAscii.RuneCount() != 13 {
		fmt.Fprintf(os.Stderr, "FAIL: expected RuneCount 13 for ascii, got %d\n", uAscii.RuneCount())
		os.Exit(1)
	}
	if !uAscii.IsASCII() {
		fmt.Fprintf(os.Stderr, "FAIL: expected IsASCII true for %q\n", asciiStr)
		os.Exit(1)
	}
	if !utf8sample.IsASCIIString(asciiStr) {
		fmt.Fprintf(os.Stderr, "FAIL: helper IsASCIIString returned false for %q\n", asciiStr)
		os.Exit(1)
	}

	// Multi-byte Unicode: "안녕하세요 🚀" (5 Korean characters + 1 space + 1 emoji = 7 runes, 20 bytes)
	multiByteStr := "안녕하세요 🚀"
	uMulti := utf8string.NewString(multiByteStr)
	if uMulti.RuneCount() != 7 {
		fmt.Fprintf(os.Stderr, "FAIL: expected RuneCount 7 for multi-byte, got %d\n", uMulti.RuneCount())
		os.Exit(1)
	}
	if uMulti.IsASCII() {
		fmt.Fprintf(os.Stderr, "FAIL: expected IsASCII false for %q\n", multiByteStr)
		os.Exit(1)
	}
	if uMulti.String() != multiByteStr {
		fmt.Fprintf(os.Stderr, "FAIL: String() mismatch, expected %q, got %q\n", multiByteStr, uMulti.String())
		os.Exit(1)
	}

	// Assertion 2: utf8string.At indexes multi-byte runes by character position without splitting multi-byte sequences
	if r := uMulti.At(0); r != '안' {
		fmt.Fprintf(os.Stderr, "FAIL: At(0) expected '안', got %c (%U)\n", r, r)
		os.Exit(1)
	}
	if r := uMulti.At(4); r != '요' {
		fmt.Fprintf(os.Stderr, "FAIL: At(4) expected '요', got %c (%U)\n", r, r)
		os.Exit(1)
	}
	if r := uMulti.At(6); r != '🚀' {
		fmt.Fprintf(os.Stderr, "FAIL: At(6) expected '🚀', got %c (%U)\n", r, r)
		os.Exit(1)
	}
	if r := utf8sample.RuneAt(multiByteStr, 6); r != '🚀' {
		fmt.Fprintf(os.Stderr, "FAIL: RuneAt helper expected '🚀', got %c (%U)\n", r, r)
		os.Exit(1)
	}

	// Assertion 3: utf8string.Slice extracts string substrings using rune boundaries rather than byte offsets
	slicedGreeting := uMulti.Slice(0, 5)
	if slicedGreeting != "안녕하세요" {
		fmt.Fprintf(os.Stderr, "FAIL: Slice(0, 5) expected '안녕하세요', got %q\n", slicedGreeting)
		os.Exit(1)
	}
	slicedEmoji := uMulti.Slice(6, 7)
	if slicedEmoji != "🚀" {
		fmt.Fprintf(os.Stderr, "FAIL: Slice(6, 7) expected '🚀', got %q\n", slicedEmoji)
		os.Exit(1)
	}

	truncated := utf8sample.TruncateRunes(multiByteStr, 2)
	if truncated != "안녕" {
		fmt.Fprintf(os.Stderr, "FAIL: TruncateRunes expected '안녕', got %q\n", truncated)
		os.Exit(1)
	}
	sub := utf8sample.SubstringByRune(multiByteStr, 2, 5)
	if sub != "하세요" {
		fmt.Fprintf(os.Stderr, "FAIL: SubstringByRune expected '하세요', got %q\n", sub)
		os.Exit(1)
	}

	// Assertion 4: utf8string.String.Init initializes an existing struct instance and supports rune counting and slicing
	var directStr utf8string.String
	directStr.Init("Alpha Beta")
	if directStr.RuneCount() != 10 || directStr.Slice(6, 10) != "Beta" {
		fmt.Fprintf(os.Stderr, "FAIL: direct Init() failed\n")
		os.Exit(1)
	}

	fmt.Println("PASS: golang.org/x/exp/utf8string contract verified")
}
utf8helper.go
package utf8sample

import (
	"golang.org/x/exp/utf8string"
)

// TruncateRunes truncates a UTF-8 string to maxRunes without splitting multi-byte runes.
func TruncateRunes(s string, maxRunes int) string {
	u := utf8string.NewString(s)
	if u.RuneCount() <= maxRunes {
		return u.String()
	}
	return u.Slice(0, maxRunes)
}

// SubstringByRune extracts runes in [start, end) range safely.
func SubstringByRune(s string, start, end int) string {
	u := utf8string.NewString(s)
	count := u.RuneCount()
	if start < 0 {
		start = 0
	}
	if start > count {
		start = count
	}
	if end < start {
		end = start
	}
	if end > count {
		end = count
	}
	return u.Slice(start, end)
}

// RuneAt returns the rune at rune-index i.
func RuneAt(s string, i int) rune {
	u := utf8string.NewString(s)
	return u.At(i)
}

// IsASCIIString checks if the string contains only ASCII characters.
func IsASCIIString(s string) bool {
	u := utf8string.NewString(s)
	return u.IsASCII()
}

原始种子者

匿名