This specification defines how to implement
general-purpose Kana transliterators.

* Conformance

A conforming implementation
[MUST[MUST]] use 
the steps to
[[convert a string into Katakana]]
to implement Hiragana to Katakana conversions,
or
the steps to
[[convert a string into Hiragana]]
to implement Katakana to Hiragana conversions.

* Hiragana to Katakana

To [DFN[convert a string into Katakana]] with string [VAR[input]],
run these steps:

[FIG(steps)[
= Return the result of running the steps to
[[transliterate a string]] [VAR[input]] with the [[Hiragana to Katakana table]].
]FIG]

* Katakana to Hiragana

To [DFN[convert a string into Hiragana]] with string [VAR[input]],
run these steps:

[FIG(steps)[
= Return the result of running the steps to
[[transliterate a string]] [VAR[input]] with the [[Katakana to Hiragana table]].
]FIG]

* Normalization

To [DFN[normalize a Kana string]] [VAR[input]], run these steps:

[FIG(steps)[
= Let [VAR[output]] be the empty [[string]].
= Let [VAR[length]] be the [[length]] of [VAR[input]].
= Let [VAR[mapping table]] be the [[Normalization table]].
= Let [VAR[i]] be zero.
= [[While]] [VAR[i]] is less than [VAR[length]]:
== Let [VAR[char]] be [VAR[i]]th [[code point]] within [VAR[input]].
== Let [VAR[mark]] be the empty [[string]].
== If [VAR[i]] + 1 is less than [VAR[length]]:
=== Set [VAR[mark]] to ([VAR[i]] + 1)th [[code point]] within [VAR[input]].
== If [VAR[mark]] is not the empty [[string]] and 
[VAR[mapping table]] [ [VAR[char]] followed by [VAR[mark]] ] [[exists]]:
=== [[Append][append]] [VAR[mapping table]] [ [VAR[char]] followed by [VAR[mark]] ] to [VAR[output]].
=== Increment [VAR[i]] by two.
== Otherwise, if [VAR[mapping table]] [ [VAR[char]] ] [[exists]]:
=== [[Append][append]] [VAR[mapping table]] [ [VAR[char]] ] to [VAR[output]].
=== Increment [VAR[i]] by one.
== Otherwise:
=== [[Append][append]] [VAR[char]] to [VAR[output]].
=== Increment [VAR[i]] by one.
= Return [VAR[output]].

]FIG]

* Algorithm

To [DFN[transliterate a string]]
[VAR[input]] with [VAR[mapping table]],
run these steps:

[FIG(steps)[
= Let [VAR[output]] be the empty [[string]].
= [[For each]] [[code point]] [VAR[char]] in [VAR[input]]:
== If [VAR[mapping table]] [ [VAR[char]] ] [[exists]]:
=== [[Append][append]] [VAR[mapping table]] [ [VAR[char]] ] to [VAR[output]].
== Otherwise:
=== [[Append][append]] [VAR[char]] to [VAR[output]].
= Return [VAR[output]].
]FIG]

;; A [VAR[mapping table]] [ [VAR[char]] ] value can have more than one
[[code points][code point]].

* Tables

Tables referenced from this specification are defined in the [CODE[maps.json]]
data file 
<https://github.com/manakai/data-chars/blob/master/data/maps.json>
(documentation: <https://github.com/manakai/data-chars/blob/master/doc/maps.txt>),
which is a normative part of this specification.

The JSON data file contains several mapping tables, identified by
a name, containing entries from code points to code points.
For the purpose of this specification, 
they are considered as [[maps][map]] of 
[[string]] to [[string]] [[entries][entry]]
with following names:

[FIG(table)[
:json: Name in JSON data file
:this: Name in this specification

:json: [CODE[kana:h2k]]
:this: [DFN[Hiragana to Katakana table]]

:json: [CODE[kana:k2h]]
:this: [DFN[Katakana to Hiragana table]]

:json: [CODE[kana:normalization]]
:this: [DFN[Normalization table]]

]FIG]

;; Any other mapping tables in the JSON data file are not used by this specification.

These tables will be updated when new Kana characters are added to
the [CITE[Unicode Standard]].  Implementations [SHOULD[SHOULD]]
be prepared for updating their tables.

* References

This specification depends on the [CITE[Infra Standard]]
<https://infra.spec.whatwg.org/>.
The terms
[DFN[for each][For each]],
[DFN[while][While]],
[DFN[code point]],
[DFN[length]],
[DFN[string]],
[DFN[append]],
[DFN[ordered map][map]],
[DFN[entry]],
and
[DFN[exists]]
are defined by the [CITE[Infra Standard]].

* Test data

[I[This section is non-normative.]]

There are test data:

- <https://github.com/manakai/data-chars/blob/master/data/tests/kana-tokana.json>
- <https://github.com/manakai/data-chars/blob/master/data/tests/kana-normalize.json>
- Documentation:
<https://github.com/manakai/data-chars/blob/master/data/tests/kana-tests.txt>


* License

Per CC0 <https://creativecommons.org/publicdomain/zero/1.0/>, to the extent possible under law, the author of this specification has waived all copyright and related or neighboring rights to this specification.

* Notes

[I[This section is non-normative.]]

See [[平仮名と片仮名]] for rationale.