/** * Takes a Stringset and an array of options and creates a po formatted string. * @param Stringset $set * @return string */ public static function toString(Stringset $set) { $str = ''; for ($i = 0; $i < $set->size(); $i += 1) { $item = $set->item($i); if (count($item['flags']) > 0) { $str .= "#, " . implode(", ", $item['flags']) . "\n"; } if ($item['context'] !== null) { $str .= "msgctxt " . '"' . $item['context'] . '"' . "\n"; } $str .= "msgid " . '"' . self::escapeString($item['id']) . '"' . "\n"; if ($item['plural'] !== null) { $str .= "msgid_plural " . '"' . self::escapeString($item['plural']) . '"' . "\n"; } if (count($item['strings']) === 1) { $str .= "msgstr " . '"' . self::escapeString($item['strings'][0]) . '"' . "\n"; } else { for ($j = 0; $j < count($item['strings']); $j += 1) { $str .= "msgstr[" . $i . "] " . '"' . self::escapeString($item['strings'][0]) . '"' . "\n"; } } $str .= "\n"; } return $str; }
/** * Generate the hashtable. * Uses an array which is... a hashtable. Clearly this is a * case of hashtableception. * @param Stringset $set * @param integer $size * @return integer[] */ private static function makeHashTable(Stringset $set, $size) { $table = array(); // by default everything points to zero for ($i = 0; $i < $size; $i += 1) { $table[$i] = 0; } for ($i = 0; $i < $set->size(); $i += 1) { $item = $set->item($i); $hash = self::hash($item['id']); $index = $hash % $size; $inc = $hash % ($size - 2) + 1; // check for collisions while ($table[$index] !== 0) { if ($index < $size - $inc) { $index += $inc; } else { // out of bounds, start at the bottom $index -= $size - $inc; } } // and insert it $table[$index] = $i + 1; } return $table; }