Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 1500] ファイルの概要

このコミットは、Go言語の標準ライブラリに含まれるregexpパッケージ内のコードの命名規則を統一し、テストコードを整理することを目的としています。具体的には、関数名や型名、定数名のキャメルケース(CamelCase)表記をアンダースコア(_)プレフィックスを用いた表記や、よりGo言語の慣習に沿った命名に変更しています。

コミット

commit 794efd7e78855a5b71ba3ccb7426e7443ff1bac8
Author: Rob Pike <r@golang.org>
Date:   Fri Jan 16 10:34:36 2009 -0800

    recasify regexp to use underscores and clean up the tests more
    
    R=rsc
    DELTA=174  (0 added, 0 deleted, 174 changed)
    OCL=22917
    CL=22942

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/794efd7e78855a5b71ba3ccb7426e7443ff1bac8

元コミット内容

recasify regexp to use underscores and clean up the tests more

変更の背景

このコミットは、Go言語がまだ初期段階にあった2009年に行われたものです。当時のGo言語のコードベースでは、命名規則がまだ完全に確立されておらず、異なるスタイルが混在していることがありました。このコミットは、regexpパッケージにおいて、より一貫性のある命名規則を適用し、特に内部的な型や定数、ヘルパー関数に対して、Go言語の慣習に沿った命名(例えば、非エクスポートの識別子にアンダースコアをプレフィックスとして使用する、あるいは小文字で始めるなど)を導入することで、コードの可読性と保守性を向上させることを目的としています。

また、テストコードの整理も行われており、テストヘルパー関数の命名もGo言語の慣習に合わせることで、テストコード自体の品質も向上させています。これは、Go言語の設計思想である「シンプルさ」と「一貫性」を追求する過程の一部と言えます。

前提知識の解説

Go言語の命名規則

Go言語には、識別子の命名に関する明確な慣習があります。

  • エクスポートされる識別子: パッケージ外からアクセス可能な関数、変数、型などは、大文字で始まる必要があります(例: Compile, Regexp)。
  • エクスポートされない識別子: パッケージ内部でのみ使用される関数、変数、型などは、小文字で始まる必要があります(例: compiler, regexp)。
  • CamelCaseとsnake_case: Go言語では、一般的に複数の単語からなる識別子にはCamelCase(例: myFunction)が推奨されます。ただし、このコミットが行われた初期の段階では、一部で異なる慣習が見られたり、内部的な識別子に対してアンダースコアをプレフィックスとして使用するスタイルが試みられたりしていました。このコミットは、その過渡期における命名規則の調整を示しています。
  • 型名と定数名: 型名は通常CamelCaseで、定数名は関連する型名や用途に応じてCamelCaseまたはALL_CAPSが使用されますが、このコミットでは内部的な定数に対してアンダースコアプレフィックスが導入されています。

正規表現(RegExp)

正規表現は、文字列のパターンを記述するための強力なツールです。Go言語のregexpパッケージは、この正規表現を扱うための機能を提供します。正規表現エンジンは、通常、正規表現を内部的な命令(Instruction)のシーケンスにコンパイルし、その命令を実行することで文字列とのマッチングを行います。このコミットで変更されているinstrインターフェースや_Char, _Altなどの型は、この正規表現エンジンの内部的な命令や構成要素を表しています。

テスト駆動開発とテストコードの重要性

ソフトウェア開発において、テストコードは機能の正しさを保証し、リファクタリングや機能追加の際の安全網となります。Go言語の標準ライブラリは、testingパッケージを提供し、シンプルなテストフレームワークを提供しています。このコミットでテストコードが整理されているのは、正規表現エンジンの複雑なロジックを確実に検証し、将来の変更に備えるためです。

技術的詳細

このコミットの主要な変更は、src/lib/regexp/all_test.gosrc/lib/regexp/regexp.goの2つのファイルにわたる命名規則の変更です。

src/lib/regexp/all_test.goの変更

このファイルはregexpパッケージのテストコードを含んでいます。変更のポイントは以下の通りです。

  • テストヘルパー関数のリネーム: CompileTestからcompileTestPrintvecからprintVecEqualからequalなど、テスト内部で使用されるヘルパー関数が、Go言語の非エクスポート識別子の慣習に従い、小文字で始まる名前に変更されています。これにより、これらの関数がパッケージ外部から直接呼び出されることを意図していないことが明確になります。
  • リネームされた関数の呼び出し箇所の更新: 上記のリネームに伴い、テストコード内でこれらのヘルパー関数を呼び出している箇所もすべて新しい名前に更新されています。

src/lib/regexp/regexp.goの変更

このファイルはregexpパッケージのコアロジックを含んでいます。変更のポイントは以下の通りです。

  • 内部型のリネーム: iCommonから_CommonregExpから_REiStartから_Startなど、正規表現エンジンの内部的な構造体やインターフェースの型名が、iプレフィックスから_プレフィックスに変更されています。これは、Go言語の初期段階で内部的な型を示すためにiが使われていたが、後に_や単に小文字で始める慣習に移行したことを示唆しています。
  • 内部定数のリネーム: cSTARTから_STARTcENDから_ENDなど、正規表現の命令タイプを表す定数名が、cプレフィックスから_プレフィックスに変更されています。これも型名と同様に、内部的な定数であることをより明確にするための変更です。
  • メソッドレシーバーの型変更: regExp型に紐づくメソッド(例: Error, Add, EliminateNops, Dump, DoParse, DoExecute, Execute, Match, MatchStrings)のレシーバーの型が*regExpから*_REに変更されています。これは、regExp型自体が_REにリネームされたことに伴う変更です。
  • 関数名のリネーム: CompilerからcompilerUnNopからunNopなど、パッケージ内部で使用される関数が小文字で始まる名前に変更されています。

これらの変更は、コードの機能的な振る舞いを変えるものではなく、純粋にコードのスタイルと命名規則を改善するためのリファクタリングです。これにより、コードベース全体の一貫性が高まり、将来のGo言語の進化に合わせた基盤が築かれました。

コアとなるコードの変更箇所

src/lib/regexp/all_test.go

--- a/src/lib/regexp/all_test.go
+++ b/src/lib/regexp/all_test.go
@@ -85,7 +85,7 @@ var matches = []tester {
 	tester{ `a*(|(b))c*`,	"aacc",	vec{0,4, 2,2, -1,-1} },
 }
 
-func CompileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
+func compileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
 	re, err := regexp.Compile(expr);
 	if err != error {
 		t.Error("compiling `", expr, "`; unexpected error: ", err.String());
@@ -93,7 +93,7 @@ func CompileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
 	return re
 }
 
-func Printvec(t *testing.T, m []int) {
+func printVec(t *testing.T, m []int) {
 	l := len(m);
 	if l == 0 {
 		t.Log("\t<no match>");
@@ -104,7 +104,7 @@ func Printvec(t *testing.T, m []int) {
 	}
 }
 
-func PrintStrings(t *testing.T, m []string) {
+func printStrings(t *testing.T, m []string) {
 	l := len(m);
 	if l == 0 {
 		t.Log("\t<no match>");
@@ -115,7 +115,7 @@ func PrintStrings(t *testing.T, m []string) {
 	}
 }
 
-func Equal(m1, m2 []int) bool {
+func equal(m1, m2 []int) bool {
 	l := len(m1);
 	if l != len(m2) {
 		return false
@@ -128,7 +128,7 @@ func Equal(m1, m2 []int) bool {
 	return true
 }
 
-func EqualStrings(m1, m2 []string) bool {
+func equalStrings(m1, m2 []string) bool {
 	l := len(m1);
 	if l != len(m2) {
 		return false
@@ -141,41 +141,41 @@ func EqualStrings(m1, m2 []string) bool {
 	return true
 }
 
-func ExecuteTest(t *testing.T, expr string, str string, match []int) {
-	re := CompileTest(t, expr, nil);
+func executeTest(t *testing.T, expr string, str string, match []int) {
+	re := compileTest(t, expr, nil);
 	if re == nil {
 		return
 	}
 	m := re.Execute(str);
-	if !Equal(m, match) {
+	if !equal(m, match) {
 		t.Error("Execute failure on `", expr, "` matching `", str, "`:");
-		Printvec(t, m);
+		printVec(t, m);
 		t.Log("should be:");
-		Printvec(t, match);
+		printVec(t, match);
 	}
 }
 
 export func TestGoodCompile(t *testing.T) {
 	for i := 0; i < len(good_re); i++ {
-		CompileTest(t, good_re[i], nil);
+		compileTest(t, good_re[i], nil);
 	}
 }
 
 export func TestBadCompile(t *testing.T) {
 	for i := 0; i < len(bad_re); i++ {
-		CompileTest(t, bad_re[i].re, bad_re[i].err)
+		compileTest(t, bad_re[i].re, bad_re[i].err)
 	}
 }
 
 export func TestExecute(t *testing.T) {
 	for i := 0; i < len(matches); i++ {
 		test := &matches[i];
-		ExecuteTest(t, test.re, test.text, test.match)
+		executeTest(t, test.re, test.text, test.match)
 	}
 }
 
-func MatchTest(t *testing.T, expr string, str string, match []int) {
-	re := CompileTest(t, expr, nil);
+func matchTest(t *testing.T, expr string, str string, match []int) {
+	re := compileTest(t, expr, nil);
 	if re == nil {
 		return
 	}
@@ -188,12 +188,12 @@ func MatchTest(t *testing.T, expr string, str string, match []int) {
 export func TestMatch(t *testing.T) {
 	for i := 0; i < len(matches); i++ {
 		test := &matches[i];
-		MatchTest(t, test.re, test.text, test.match)
+		matchTest(t, test.re, test.text, test.match)
 	}
 }
 
-func MatchStringsTest(t *testing.T, expr string, str string, match []int) {
-	re := CompileTest(t, expr, nil);
+func matchStringsTest(t *testing.T, expr string, str string, match []int) {
+	re := compileTest(t, expr, nil);
 	if re == nil {
 		return
 	}
@@ -202,22 +202,22 @@ func MatchStringsTest(t *testing.T, expr string, str string, match []int) {
 		strs[i/2] = str[match[i] : match[i+1]]
 	}
 	m := re.MatchStrings(str);
-	if !EqualStrings(m, strs) {
+	if !equalStrings(m, strs) {
 		t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:");
-		PrintStrings(t, m);
+		printStrings(t, m);
 		t.Log("should be:");
-		PrintStrings(t, strs);
+		printStrings(t, strs);
 	}
 }
 
 export func TestMatchStrings(t *testing.T) {
 	for i := 0; i < len(matches); i++ {
 		test := &matches[i];
-		MatchTest(t, test.re, test.text, test.match)
+		matchTest(t, test.re, test.text, test.match)
 	}
 }
 
-func MatchFunctionTest(t *testing.T, expr string, str string, match []int) {
+func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
 	m, err := Match(expr, str);
 	if err == nil {
 		return
@@ -230,6 +230,6 @@ func MatchFunctionTest(t *testing.T, expr string, str string, match []int) {
 export func TestMatchFunction(t *testing.T) {
 	for i := 0; i < len(matches); i++ {
 		test := &matches[i];
-		MatchFunctionTest(t, test.re, test.text, test.match)
+		matchFunctionTest(t, test.re, test.text, test.match)
 	}
 }

src/lib/regexp/regexp.go

--- a/src/lib/regexp/regexp.go
+++ b/src/lib/regexp/regexp.go
@@ -27,7 +27,7 @@ export var ErrBadBackslash = os.NewError("illegal backslash escape");
 
 // An instruction executed by the NFA
 type instr interface {
-	Type()	int;	// the type of this instruction: cCHAR, cANY, etc.
+	Type()	int;	// the type of this instruction: _CHAR, _ANY, etc.
 	Next()	instr;	// the instruction to execute after this one
 	SetNext(i instr);
 	Index()	int;
@@ -36,19 +36,19 @@ type instr interface {
 }
 
 // Fields and methods common to all instructions
-type iCommon struct {
+type _Common struct {
 	next	instr;
 	index	int;
 }
 
-func (c *iCommon) Next() instr { return c.next }
-func (c *iCommon) SetNext(i instr) { c.next = i }
-func (c *iCommon) Index() int { return c.index }
-func (c *iCommon) SetIndex(i int) { c.index = i }
+func (c *_Common) Next() instr { return c.next }
+func (c *_Common) SetNext(i instr) { c.next = i }
+func (c *_Common) Index() int { return c.index }
+func (c *_Common) SetIndex(i int) { c.index = i }
 
-type regExp struct {
+type _RE struct {
 	expr	string;	// the original expression
-	ch	chan<- *regExp;	// reply channel when we're done
+	ch	chan<- *_RE;	// reply channel when we're done
 	error	*os.Error;	// compile- or run-time error; nil if OK
 	inst	*array.Array;
 	start	instr;
@@ -56,80 +56,80 @@ type regExp struct {
 }
 
 const (
-	cSTART	// beginning of program
+	_START	// beginning of program
 		= iota;
-	cEND;		// end of program: success
-	cBOT;		// '^' beginning of text
-	cEOT;		// '$' end of text
-	cCHAR;	// 'a' regular character
-	cCHARCLASS;	// [a-z] character class
-	cANY;		// '.' any character
-	cBRA;		// '(' parenthesized expression
-	cEBRA;	// ')'; end of '(' parenthesized expression
-	cALT;		// '|' alternation
-	cNOP;		// do nothing; makes it easy to link without patching
+	_END;		// end of program: success
+	_BOT;		// '^' beginning of text
+	_EOT;		// '$' end of text
+	_CHAR;	// 'a' regular character
+	_CHARCLASS;	// [a-z] character class
+	_ANY;		// '.' any character
+	_BRA;		// '(' parenthesized expression
+	_EBRA;	// ')'; end of '(' parenthesized expression
+	_ALT;		// '|' alternation
+	_NOP;		// do nothing; makes it easy to link without patching
 )
 
 // --- START start of program
-type iStart struct {
-	iCommon
+type _Start struct {
+	_Common
 }
 
-func (start *iStart) Type() int { return cSTART }
-func (start *iStart) Print() { print("start") }
+func (start *_Start) Type() int { return _START }
+func (start *_Start) Print() { print("start") }
 
 // --- END end of program
-type iEnd struct {
-	iCommon
+type _End struct {
+	_Common
 }
 
-func (end *iEnd) Type() int { return cEND }
-func (end *iEnd) Print() { print("end") }
+func (end *_End) Type() int { return _END }
+func (end *_End) Print() { print("end") }
 
 // --- BOT beginning of text
-type iBot struct {
-	iCommon
+type _Bot struct {
+	_Common
 }
 
-func (bot *iBot) Type() int { return cBOT }
-func (bot *iBot) Print() { print("bot") }
+func (bot *_Bot) Type() int { return _BOT }
+func (bot *_Bot) Print() { print("bot") }
 
 // --- EOT end of text
-type iEot struct {
-	iCommon
+type _Eot struct {
+	_Common
 }
 
-func (eot *iEot) Type() int { return cEOT }
-func (eot *iEot) Print() { print("eot") }
+func (eot *_Eot) Type() int { return _EOT }
+func (eot *_Eot) Print() { print("eot") }
 
 // --- CHAR a regular character
-type iChar struct {
-	iCommon;
+type _Char struct {
+	_Common;
 	char	int;
 }
 
-func (char *iChar) Type() int { return cCHAR }
-func (char *iChar) Print() { print("char ", string(char.char)) }
+func (char *_Char) Type() int { return _CHAR }
+func (char *_Char) Print() { print("char ", string(char.char)) }
 
-func newChar(char int) *iChar {
-	c := new(iChar);
+func newChar(char int) *_Char {
+	c := new(_Char);
 	c.char = char;
 	return c;
 }
 
 // --- CHARCLASS [a-z]
 
-type iCharClass struct {
-	iCommon;
+type _CharClass struct {
+	_Common;
 	char	int;
 	negate	bool;	// is character class negated? ([^a-z])
 	// array of int, stored pairwise: [a-z] is (a,z); x is (x,x):
 	ranges	*array.IntArray;
 }
 
-func (cclass *iCharClass) Type() int { return cCHARCLASS }
+func (cclass *_CharClass) Type() int { return _CHARCLASS }
 
-func (cclass *iCharClass) Print() {
+func (cclass *_CharClass) Print() {
 	print("charclass");
 	if cclass.negate {
 		print(" (negated)");
@@ -145,13 +145,13 @@ func (cclass *iCharClass) Print() {
 	}
 }
 
-func (cclass *iCharClass) AddRange(a, b int) {
+func (cclass *_CharClass) AddRange(a, b int) {
 	// range is a through b inclusive
 	cclass.ranges.Push(a);
 	cclass.ranges.Push(b);
 }
 
-func (cclass *iCharClass) Matches(c int) bool {
+func (cclass *_CharClass) Matches(c int) bool {
 	for i := 0; i < cclass.ranges.Len(); i = i+2 {
 		min := cclass.ranges.At(i);
 		max := cclass.ranges.At(i+1);
@@ -162,70 +162,70 @@ func (cclass *iCharClass) Matches(c int) bool {
 	return cclass.negate
 }
 
-func newCharClass() *iCharClass {
-	c := new(iCharClass);
+func newCharClass() *_CharClass {
+	c := new(_CharClass);
 	c.ranges = array.NewIntArray(0);
 	return c;
 }
 
 // --- ANY any character
-type iAny struct {
-	iCommon
+type _Any struct {
+	_Common
 }
 
-func (any *iAny) Type() int { return cANY }
-func (any *iAny) Print() { print("any") }
+func (any *_Any) Type() int { return _ANY }
+func (any *_Any) Print() { print("any") }
 
 // --- BRA parenthesized expression
-type iBra struct {
-	iCommon;
+type _Bra struct {
+	_Common;
 	n	int;	// subexpression number
 }
 
-func (bra *iBra) Type() int { return cBRA }
-func (bra *iBra) Print() { print("bra", bra.n); }
+func (bra *_Bra) Type() int { return _BRA }
+func (bra *_Bra) Print() { print("bra", bra.n); }
 
 // --- EBRA end of parenthesized expression
-type iEbra struct {
-	iCommon;
+type _Ebra struct {
+	_Common;
 	n	int;	// subexpression number
 }
 
-func (ebra *iEbra) Type() int { return cEBRA }
-func (ebra *iEbra) Print() { print("ebra ", ebra.n); }
+func (ebra *_Ebra) Type() int { return _EBRA }
+func (ebra *_Ebra) Print() { print("ebra ", ebra.n); }
 
 // --- ALT alternation
-type iAlt struct {
-	iCommon;
+type _Alt struct {
+	_Common;
 	left	instr;	// other branch
 }
 
-func (alt *iAlt) Type() int { return cALT }
-func (alt *iAlt) Print() { print("alt(", alt.left.Index(), ")"); }
+func (alt *_Alt) Type() int { return _ALT }
+func (alt *_Alt) Print() { print("alt(", alt.left.Index(), ")"); }
 
 // --- NOP no operation
-type iNop struct {
-	iCommon
+type _Nop struct {
+	_Common
 }
 
-func (nop *iNop) Type() int { return cNOP }
-func (nop *iNop) Print() { print("nop") }
+func (nop *_Nop) Type() int { return _NOP }
+func (nop *_Nop) Print() { print("nop") }
 
 // report error and exit compiling/executing goroutine
-func (re *regExp) Error(err *os.Error) {
+func (re *_RE) Error(err *os.Error) {
 	re.error = err;
 	re.ch <- re;
 	sys.goexit();
 }
 
-func (re *regExp) Add(i instr) instr {
+func (re *_RE) Add(i instr) instr {
 	i.SetIndex(re.inst.Len());
 	re.inst.Push(i);
 	return i;
 }
 
 type parser struct {
-	re	*regExp;
+	re	*_RE;
 	nlpar	int;	// number of unclosed lpars
 	pos	int;
 	ch	int;
@@ -248,7 +248,7 @@ func (p *parser) nextc() int {
 	return p.ch;
 }
 
-func newParser(re *regExp) *parser {
+func newParser(re *_RE) *parser {
 	p := new(parser);
 	p.re = re;
 	p.nextc();	// load p.ch
@@ -364,15 +364,15 @@ func (p *parser) Term() (start, end instr) {
 		p.re.Error(ErrUnmatchedRbkt);
 	case '^':
 		p.nextc();
-		start = p.re.Add(new(iBot));
+		start = p.re.Add(new(_Bot));
 		return start, start;
 	case '$':
 		p.nextc();
-		start = p.re.Add(new(iEot));
+		start = p.re.Add(new(_Eot));
 		return start, start;
 	case '.':
 		p.nextc();
-		start = p.re.Add(new(iAny));
+		start = p.re.Add(new(_Any));
 		return start, start;
 	case '[':
 		p.nextc();
@@ -393,9 +393,9 @@ func (p *parser) Term() (start, end instr) {
 		}
 		p.nlpar--;
 		p.nextc();
-		bra := new(iBra);
+		bra := new(_Bra);
 		p.re.Add(bra);
-		ebra := new(iEbra);
+		ebra := new(_Ebra);
 		p.re.Add(ebra);
 		bra.n = nbra;
 		ebra.n = nbra;
@@ -437,7 +437,7 @@ func (p *parser) Closure() (start, end instr) {
 	switch p.c() {
 	case '*':
 		// (start,end)*:
-		alt := new(iAlt);
+		alt := new(_Alt);
 		p.re.Add(alt);
 		end.SetNext(alt);	// after end, do alt
 		alt.left = start;	// alternate brach: return to start
@@ -445,16 +445,16 @@ func (p *parser) Closure() (start, end instr) {
 		end = alt;
 	case '+':
 		// (start,end)+:
-		alt := new(iAlt);
+		alt := new(_Alt);
 		p.re.Add(alt);
 		end.SetNext(alt);	// after end, do alt
 		alt.left = start;	// alternate brach: return to start
 		end = alt;	// start is unchanged; end is alt
 	case '?':
 		// (start,end)?:
-		alt := new(iAlt);
+		alt := new(_Alt);
 		p.re.Add(alt);
-		nop := new(iNop);
+		nop := new(_Nop);
 		p.re.Add(nop);
 		alt.left = start;	// alternate branch is start
 		alt.next = nop;	// follow on to nop
@@ -478,7 +478,7 @@ func (p *parser) Concatenation() (start, end instr) {
 		switch {
 		case nstart == iNULL:	// end of this concatenation
 			if start == iNULL {	// this is the empty string
-				nop := p.re.Add(new(iNop));
+				nop := p.re.Add(new(_Nop));
 				return nop, nop;
 			}
 			return;
@@ -501,11 +501,11 @@ func (p *parser) Regexp() (start, end instr) {
 		case '|':
 			p.nextc();
 			nstart, nend := p.Concatenation();
-			alt := new(iAlt);
+			alt := new(_Alt);
 			p.re.Add(alt);
 			alt.left = start;
 			alt.next = nstart;
-			nop := new(iNop);
+			nop := new(_Nop);
 			p.re.Add(nop);
 			end.SetNext(nop);
 			nend.SetNext(nop);
@@ -515,47 +515,47 @@ func (p *parser) Regexp() (start, end instr) {
 	panic("unreachable");
 }
 
-func UnNop(i instr) instr {
-	for i.Type() == cNOP {
+func unNop(i instr) instr {
+	for i.Type() == _NOP {
 		i = i.Next()
 	}
 	return i
 }
 
-func (re *regExp) EliminateNops() {
+func (re *_RE) EliminateNops() {
 	for i := 0; i < re.inst.Len(); i++ {
 		inst := re.inst.At(i).(instr);
-		if inst.Type() == cEND {
+		if inst.Type() == _END {
 			continue
 		}
-		inst.SetNext(UnNop(inst.Next()));
-		if inst.Type() == cALT {
-			alt := inst.(*iAlt);
-			alt.left = UnNop(alt.left);
+		inst.SetNext(unNop(inst.Next()));
+		if inst.Type() == _ALT {
+			alt := inst.(*_Alt);
+			alt.left = unNop(alt.left);
 		}
 	}
 }
 
-func (re *regExp) Dump() {
+func (re *_RE) Dump() {
 	for i := 0; i < re.inst.Len(); i++ {
 		inst := re.inst.At(i).(instr);
 		print(inst.Index(), ": ");
 		inst.Print();
-		if inst.Type() != cEND {
+		if inst.Type() != _END {
 			print(" -> ", inst.Next().Index())
 		}
 		print("\n");
 	}
 }
 
-func (re *regExp) DoParse() {
+func (re *_RE) DoParse() {
 	p := newParser(re);
-	start := new(iStart);
+	start := new(_Start);
 	re.Add(start);
 	s, e := p.Regexp();
 	start.next = s;
 	re.start = start;
-	e.SetNext(re.Add(new(iEnd)));
+	e.SetNext(re.Add(new(_End)));
 
 	if debug {
 		re.Dump();
@@ -571,8 +571,8 @@ func (re *regExp) DoParse() {
 }
 
 
-func Compiler(str string, ch chan *regExp) {
-	re := new(regExp);
+func compiler(str string, ch chan *_RE) {
+	re := new(_RE);
 	re.expr = str;
 	re.inst = array.New(0);
 	re.ch = ch;
@@ -589,8 +589,8 @@ export type Regexp interface {
 
 // Compile in separate goroutine; wait for result
 export func Compile(str string) (regexp Regexp, error *os.Error) {
-	ch := make(chan *regExp);
-	go Compiler(str, ch);
+	ch := make(chan *_RE);
+	go compiler(str, ch);
 	re := <-ch;
 	return re, re.error
 }
@@ -627,7 +627,7 @@ func addState(s []state, inst instr, match []int) []state {
 	return s;
 }
 
-func (re *regExp) DoExecute(str string, pos int) []int {
+func (re *_RE) DoExecute(str string, pos int) []int {
 	var s [2][]state;	// TODO: use a vector when state values (not ptrs) can be vector elements
 	s[0] = make([]state, 10)[0:0];
 	s[1] = make([]state, 10)[0:0];
@@ -658,43 +658,43 @@ func (re *regExp) DoExecute(str string, pos int) []int {
 		for i := 0; i < len(s[in]); i++ {
 			st := s[in][i];
 			switch s[in][i].inst.Type() {
-			case cBOT:
+			case _BOT:
 				if pos == 0 {
 					s[in] = addState(s[in], st.inst.Next(), st.match)
 				}
-			case cEOT:
+			case _EOT:
 				if pos == len(str) {
 					s[in] = addState(s[in], st.inst.Next(), st.match)
 				}
-			case cCHAR:
-				if c == st.inst.(*iChar).char {
+			case _CHAR:
+				if c == st.inst.(*_Char).char {
 					s[out] = addState(s[out], st.inst.Next(), st.match)
 				}
-			case cCHARCLASS:
-				if st.inst.(*iCharClass).Matches(c) {
+			case _CHARCLASS:
+				if st.inst.(*_CharClass).Matches(c) {
 					s[out] = addState(s[out], st.inst.Next(), st.match)
 				}
-			case cANY:
+			case _ANY:
 				if c != endOfFile {
 					s[out] = addState(s[out], st.inst.Next(), st.match)
 				}
-			case cBRA:
-				n := st.inst.(*iBra).n;
+			case _BRA:
+				n := st.inst.(*_Bra).n;
 				st.match[2*n] = pos;
 				s[in] = addState(s[in], st.inst.Next(), st.match);
-			case cEBRA:
-				n := st.inst.(*iEbra).n;
+			case _EBRA:
+				n := st.inst.(*_Ebra).n;
 				st.match[2*n+1] = pos;
 				s[in] = addState(s[in], st.inst.Next(), st.match);
-			case cALT:
-				s[in] = addState(s[in], st.inst.(*iAlt).left, st.match);
+			case _ALT:
+				s[in] = addState(s[in], st.inst.(*_Alt).left, st.match);
 				// give other branch a copy of this match vector
 				s1 := make([]int, 2*(re.nbra+1));
 				for i := 0; i < len(s1); i++ {
 					s1[i] = st.match[i]
 				}
 				s[in] = addState(s[in], st.inst.Next(), s1);
-			case cEND:
+			case _END:
 				// choose leftmost longest
 				if !found ||	// first
 				   st.match[0] < final.match[0] ||	// leftmost
@@ -714,17 +714,17 @@ func (re *regExp) DoExecute(str string, pos int) []int {
 }
 
 
-func (re *regExp) Execute(s string) []int {
+func (re *_RE) Execute(s string) []int {
 	return re.DoExecute(s, 0)
 }
 
 
-func (re *regExp) Match(s string) bool {
+func (re *_RE) Match(s string) bool {
 	return len(re.DoExecute(s, 0)) > 0
 }
 
 
-func (re *regExp) MatchStrings(s string) []string {
+func (re *_RE) MatchStrings(s string) []string {
 	r := re.DoExecute(s, 0);
 	if r == nil {
 		return nil

コアとなるコードの解説

このコミットは、Go言語のregexpパッケージにおける命名規則の統一とテストコードの整理に焦点を当てています。

テストコードの整理 (all_test.go)

all_test.goでは、テストヘルパー関数(例: CompileTest, Printvec, Equalなど)が、Go言語の非エクスポート識別子の慣習に従い、小文字で始まる名前に変更されました(例: compileTest, printVec, equal)。これにより、これらの関数がパッケージ内部でのみ使用されることが明確になり、コードの意図がより伝わりやすくなりました。また、これらの関数の呼び出し箇所もすべて新しい名前に更新されており、テストコード全体の一貫性が保たれています。

正規表現エンジンの内部構造の命名変更 (regexp.go)

regexp.goでは、正規表現エンジンの内部的な型や定数、関数名が変更されています。

  • 型名の変更: iCommonから_CommonregExpから_REiStartから_Startなど、内部的な型名がiプレフィックスから_プレフィックスに変更されました。これは、Go言語の初期段階で内部的な型を示すためにiが使われていたが、後に_や単に小文字で始める慣習に移行したことを反映しています。_プレフィックスは、その識別子が内部的な実装の詳細であり、外部から直接参照されるべきではないことを示唆しています。
  • 定数名の変更: cSTARTから_STARTcENDから_ENDなど、正規表現の命令タイプを表す定数名がcプレフィックスから_プレフィックスに変更されました。これも型名と同様に、内部的な定数であることをより明確にするための変更です。
  • メソッドレシーバーの型変更: regExp型が_REにリネームされたことに伴い、その型に紐づくすべてのメソッドのレシーバーの型も*regExpから*_REに変更されました。
  • 関数名の変更: CompilerからcompilerUnNopからunNopなど、パッケージ内部で使用される関数が小文字で始まる名前に変更されました。これにより、これらの関数がパッケージ外部に公開されていないことが明確になります。

これらの変更は、コードの機能的な振る舞いには影響を与えません。しかし、Go言語のコードベース全体で一貫した命名規則を確立し、コードの可読性と保守性を向上させる上で重要なステップでした。特に、Go言語の初期段階における命名規則の試行錯誤と、最終的に確立された慣習への移行を示す良い例となっています。

関連リンク

  • Go言語の公式ドキュメント(命名規則に関する情報が含まれる可能性があります): https://go.dev/doc/
  • Go言語の正規表現パッケージのドキュメント: https://pkg.go.dev/regexp

参考にした情報源リンク

  • Go言語の公式リポジトリのコミット履歴: https://github.com/golang/go/commits/master
  • Go言語の命名規則に関する一般的なガイドライン(Go言語の進化とともに変化する可能性があるため、当時の慣習を理解するために参照)