$to = trim( $v[1] );
$v = trim( $v[0] );
$u = explode( '=>', $v, 2 );
- // if $to is empty, strtr() could return a wrong result
- if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) {
+ // if $to is empty (which is also used as $from in bidtable),
+ // strtr() could return a wrong result.
+ if ( count( $u ) == 1 && $to !== '' && in_array( $v, $variants ) ) {
$bidtable[$v] = $to;
} elseif ( count( $u ) == 2 ) {
$from = trim( $u[0] );
$v = trim( $u[1] );
+ // if $from is empty, strtr() could return a wrong result.
if ( array_key_exists( $v, $unidtable )
&& !is_array( $unidtable[$v] )
- && $to
+ && $from !== ''
&& in_array( $v, $variants ) ) {
$unidtable[$v] = array( $from => $to );
- } elseif ( $to && in_array( $v, $variants ) ) {
+ } elseif ( $from !== '' && in_array( $v, $variants ) ) {
$unidtable[$v][$from] = $to;
}
}
// display current variant in bidirectional array
$disp = $this->getTextInBidtable( $variant );
// or display current variant in fallbacks
- if ( !$disp ) {
+ if ( $disp === false ) {
$disp = $this->getTextInBidtable(
$this->mConverter->getVariantFallbacks( $variant ) );
}
// or display current variant in unidirectional array
- if ( !$disp && array_key_exists( $variant, $unidtable ) ) {
+ if ( $disp === false && array_key_exists( $variant, $unidtable ) ) {
$disp = array_values( $unidtable[$variant] );
$disp = $disp[0];
}
// or display frist text under disable manual convert
- if ( !$disp && $this->mConverter->mManualLevel[$variant] == 'disable' ) {
+ if ( $disp === false && $this->mConverter->mManualLevel[$variant] == 'disable' ) {
if ( count( $bidtable ) > 0 ) {
$disp = array_values( $bidtable );
$disp = $disp[0];
&& isset( $unidtable[$v] )
) {
if ( isset( $this->mConvTable[$v] ) ) {
- $this->mConvTable[$v] = array_merge( $this->mConvTable[$v], $unidtable[$v] );
+ $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
} else {
$this->mConvTable[$v] = $unidtable[$v];
}
if ( !$this->mBidtable && !$this->mUnidtable ) {
if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) {
- // fill all variants if text in -{A/H/-|text} without rules
- foreach ( $this->mConverter->mVariants as $v ) {
- $this->mBidtable[$v] = $rules;
+ // fill all variants if text in -{A/H/-|text}- is non-empty but without rules
+ if ( $rules !== '' ) {
+ foreach ( $this->mConverter->mVariants as $v ) {
+ $this->mBidtable[$v] = $rules;
+ }
}
} elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) {
$this->mFlags = $flags = array( 'R' => true );
}
if ( $action == 'add' ) {
+ // More efficient than array_merge(), about 2.5 times.
foreach ( $pair as $from => $to ) {
- // to ensure that $from and $to not be left blank
- // so $this->translate() could always return a string
- if ( $from || $to ) {
- // more efficient than array_merge(), about 2.5 times.
- $this->mTables[$variant]->setPair( $from, $to );
- }
+ $this->mTables[$variant]->setPair( $from, $to );
}
} elseif ( $action == 'remove' ) {
$this->mTables[$variant]->removeArray( $pair );
if ( $recursive ) {
foreach ( $sublinks as $link ) {
$s = $this->parseCachedTable( $code, $link, $recursive );
- $ret = array_merge( $ret, $s );
+ $ret = $s + $ret;
}
}
</p>
!! end
+!! test
+Strings evaluating false shouldn't be ignored by Language converter (T51072)
+!! options
+language=zh variant=zh-cn
+!! input
+-{zh-cn:0;zh-sg:1;zh-tw:2;zh-hk:3}-
+!! result
+<p>0
+</p>
+!! end
+
+!! test
+Conversion rules from [numeric-only string] to [something else] (T48634)
+!! options
+language=zh variant=zh-cn
+!! input
+-{H|0=>zh-cn:B}--{H|0=>zh-cn:C;0=>zh-cn:D}--{H|0=>zh-hans:A}-012345-{A|zh-tw:0;zh-cn:E;}-012345
+!! result
+<p>D12345EE12345
+</p>
+!! end
+
+!! test
+Bidirectional converter rule entries with an empty value should be ignored (T53551)
+!! options
+language=zh variant=zh-cn
+!! input
+-{H|zh-cn:foo;zh-tw:;}-foobar
+!! result
+<p>foobar
+</p>
+!! end
+
+!! test
+Unidirectional converter rule entries with an empty "from" string should be ignored (T53551)
+!! options
+language=zh variant=zh-cn
+!! input
+-{H|=>zh-cn:foo;}-foobar
+!! result
+<p>foobar
+</p>
+!! end
+
+!! test
+Empty converter rule entries shouldn't be inserted into the conversion table (T53551)
+!! options
+language=zh variant=zh-cn
+!! input
+-{H|}-foobar
+!! result
+<p>foobar
+</p>
+!! end
+
!! test
Nested using of manual convert syntax
!! options