~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/unicode/letter.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
275
275
        }
276
276
        return r
277
277
}
 
278
 
 
279
// caseOrbit is defined in tables.go as []foldPair.  Right now all the
 
280
// entries fit in uint16, so use uint16.  If that changes, compilation
 
281
// will fail (the constants in the composite literal will not fit in uint16)
 
282
// and the types here can change to uint32.
 
283
type foldPair struct {
 
284
        From uint16
 
285
        To   uint16
 
286
}
 
287
 
 
288
// SimpleFold iterates over Unicode code points equivalent under
 
289
// the Unicode-defined simple case folding.  Among the code points
 
290
// equivalent to rune (including rune itself), SimpleFold returns the
 
291
// smallest r >= rune if one exists, or else the smallest r >= 0. 
 
292
//
 
293
// For example:
 
294
//      SimpleFold('A') = 'a'
 
295
//      SimpleFold('a') = 'A'
 
296
//
 
297
//      SimpleFold('K') = 'k'
 
298
//      SimpleFold('k') = '\u212A' (Kelvin symbol, K)
 
299
//      SimpleFold('\u212A') = 'K'
 
300
//
 
301
//      SimpleFold('1') = '1'
 
302
//
 
303
func SimpleFold(rune int) int {
 
304
        // Consult caseOrbit table for special cases.
 
305
        lo := 0
 
306
        hi := len(caseOrbit)
 
307
        for lo < hi {
 
308
                m := lo + (hi-lo)/2
 
309
                if int(caseOrbit[m].From) < rune {
 
310
                        lo = m + 1
 
311
                } else {
 
312
                        hi = m
 
313
                }
 
314
        }
 
315
        if lo < len(caseOrbit) && int(caseOrbit[lo].From) == rune {
 
316
                return int(caseOrbit[lo].To)
 
317
        }
 
318
 
 
319
        // No folding specified.  This is a one- or two-element
 
320
        // equivalence class containing rune and ToLower(rune)
 
321
        // and ToUpper(rune) if they are different from rune.
 
322
        if l := ToLower(rune); l != rune {
 
323
                return l
 
324
        }
 
325
        return ToUpper(rune)
 
326
}