Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bamboo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type IEngine interface {
SetFlag(uint)
GetInputMethod() InputMethod
ProcessKey(rune, Mode)
SetW2UMode(int)
ProcessString(string, Mode)
GetProcessedString(Mode) string
IsValid(bool) bool
Expand All @@ -59,6 +60,7 @@ type BambooEngine struct {
composition []*Transformation
inputMethod InputMethod
flags uint
w2uMode int // 0: Disabled, 1: Middle-Only, 2: Everywhere

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using magic numbers (0, 1, 2) for w2uMode makes the code harder to read and maintain. It is recommended to define named constants or a custom type for these modes to improve clarity.

}

func NewEngine(inputMethod InputMethod, flag uint) IEngine {
Expand All @@ -77,6 +79,10 @@ func (e *BambooEngine) SetFlag(flag uint) {
e.flags = flag
}

func (e *BambooEngine) SetW2UMode(mode int) {
e.w2uMode = mode
}
Comment thread
hthienloc marked this conversation as resolved.

func (e *BambooEngine) GetFlag(flag uint) uint {
return e.flags
}
Expand Down Expand Up @@ -123,7 +129,18 @@ func (e *BambooEngine) generateTransformations(composition []*Transformation, lo
// If none of the applicable_rules can actually be applied then this new
// transformation fall-backs to an APPENDING one.
transformations = generateFallbackTransformations(composition, e.getApplicableRules(lowerKey), lowerKey, isUpperCase)
if e.flags&Ew2uEnabled != 0 && lowerKey == 'w' && len(transformations) > 0 {

// Unified Modular W2U Logic
canApplyW2U := false
if e.w2uMode == 1 {
if len(composition) > 0 {
canApplyW2U = true
}
} else if e.w2uMode == 2 {
canApplyW2U = true
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic for determining canApplyW2U can be simplified for better readability.

Important: There is a potential regression here. The previous implementation checked the Ew2uEnabled flag. Since w2uMode defaults to 0 and is not initialized from the flags in NewEngine, existing users who rely on Ew2uEnabled (which is part of EstdFlags) will find the W2U feature disabled by default. Please ensure w2uMode is correctly initialized in NewEngine based on the provided flags.

Suggested change
canApplyW2U := false
if e.w2uMode == 1 {
if len(composition) > 0 {
canApplyW2U = true
}
} else if e.w2uMode == 2 {
canApplyW2U = true
}
canApplyW2U := (e.w2uMode == 2) || (e.w2uMode == 1 && len(composition) > 0)


if canApplyW2U && lowerKey == 'w' && len(transformations) > 0 {
if transformations[0].Rule.Result == 'w' {
transformations[0].Rule.Result = 'ư'
transformations[0].Rule.EffectOn = 'ư'
Expand Down
Loading