290 | | $this->pluralheader = $expr; |
| 291 | |
| 292 | // sanitize |
| 293 | if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/', $expr, $matches)) { |
| 294 | $nplurals= preg_replace( '@[^0-9]@', '', $matches[1] ); |
| 295 | $plural= preg_replace( '@[^n0-9:\(\)\?\|\&=!<>+*/\%-]@', '', $matches[2] ); |
| 296 | $expr= 'nplurals='.$nplurals.';plural='.$plural; |
| 297 | } |
| 298 | else { |
| 299 | // malformed expression, use default ($expr is not overwritten) |
| 300 | } |
| 301 | |
| 302 | // add parentheses |
| 303 | // important since PHP's ternary evaluates from left to right |
| 304 | $expr.= ';'; |
| 305 | $res= ''; |
| 306 | $p= 0; |
| 307 | for ($i= 0; $i < strlen($expr); $i++) { |
| 308 | $ch= $expr[$i]; |
| 309 | switch ($ch) { |
| 310 | case '?': |
| 311 | $res.= ' ? ('; |
| 312 | $p++; |
| 313 | break; |
| 314 | case ':': |
| 315 | $res.= ') : ('; |
| 316 | break; |
| 317 | case ';': |
| 318 | $res.= str_repeat( ')', $p) . ';'; |
| 319 | $p= 0; |
| 320 | break; |
| 321 | default: |
| 322 | $res.= $ch; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | $this->pluralheader = $res; |