summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Locale/Codes.pm
blob: 80a859d7f8afd42744e6ddec696f5665400b563c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package Locale::Codes;
# Copyright (C) 2001      Canon Research Centre Europe (CRE).
# Copyright (C) 2002-2009 Neil Bowers
# Copyright (c) 2010-2018 Sullivan Beck
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

###############################################################################

use strict;
use warnings;
require 5.006;

use Carp;
use if $] >= 5.027007, 'deprecate';
use Locale::Codes::Constants;

our($VERSION);
$VERSION='3.56';

use Exporter qw(import);
our(@EXPORT_OK,%EXPORT_TAGS);
@EXPORT_OK   = @Locale::Codes::Constants::CONSTANTS;
%EXPORT_TAGS = ( 'constants' => [ @EXPORT_OK ] );

###############################################################################
# GLOBAL DATA
###############################################################################
# All of the data is stored in a couple global variables.  They are filled
# in by requiring the appropriate TYPE_Codes and TYPE_Retired modules.

our(%Data,%Retired);

# $Data{ TYPE }{ code2id   }{ CODESET } { CODE }  = [ ID, I ]
#              { id2code   }{ CODESET } { ID }    = CODE
#              { id2names  }{ ID }                = [ NAME, NAME, ... ]
#              { alias2id  }{ NAME }              = [ ID, I ]
#              { id        }                      = FIRST_UNUSED_ID
#              { codealias }{ CODESET } { ALIAS } = CODE
#
# $Retired{ TYPE }{ CODESET }{ code }{ CODE } = NAME
#                            { name }{ NAME } = [CODE,NAME]  (the key is lowercase)

###############################################################################
# METHODS
###############################################################################

sub new {
   my($class,$type,$codeset,$show_errors) = @_;
   my $self         = { 'type'     => '',
                        'codeset'  => '',
                        'err'      => (defined($show_errors) ? $show_errors : 1),
                      };

   bless $self,$class;

   $self->type($type)        if ($type);
   $self->codeset($codeset)  if ($codeset);
   return $self;
}

sub show_errors {
   my($self,$val) = @_;
   $$self{'err'}  = $val;
}

sub type {
   my($self,$type) = @_;

   if (! exists $ALL_CODESETS{$type}) {
      # uncoverable branch false
      carp "ERROR: type: invalid argument: $type\n"  if ($$self{'err'});
      return;
   }

   # uncoverable branch false
   if (! $ALL_CODESETS{$type}{'loaded'}) {
      my $label = $ALL_CODESETS{$type}{'module'};
      eval "require Locale::Codes::${label}_Codes";
      # uncoverable branch true
      if ($@) {
         # uncoverable statement
         croak "ERROR: type: unable to load module: ${label}_Codes\n";
      }
      eval "require Locale::Codes::${label}_Retired";
      # uncoverable branch true
      if ($@) {
         # uncoverable statement
         croak "ERROR: type: unable to load module: ${label}_Retired\n";
      }
      $ALL_CODESETS{$type}{'loaded'} = 1;
   }

   $$self{'type'}    = $type;
   $$self{'codeset'} = $ALL_CODESETS{$type}{'default'};
}

sub codeset {
   my($self,$codeset) = @_;

   my $type           = $$self{'type'};
   if (! exists $ALL_CODESETS{$type}{'codesets'}{$codeset}) {
      # uncoverable branch false
      carp "ERROR: codeset: invalid argument: $codeset\n"  if ($$self{'err'});
   }

   $$self{'codeset'}  = $codeset;
}

sub version {
  # uncoverable subroutine
  # uncoverable statement
  my($self) = @_;
  # uncoverable statement
  return $VERSION;
}

###############################################################################

# This is used to validate a codeset and/or code.  It will also format
# a code for that codeset.
#
# (ERR,RET_CODE,RET_CODESET) = $o->_code([CODE [,CODESET]])
#
#    If CODE is empty/undef, only the codeset will be validated
#    and RET_CODE will be empty.
#
#    If CODE is passed in, it will be returned formatted correctly
#    for the codeset.
#
#    ERR will be 0 or 1.
#
#    If $no_check_code is 1, then the code will not be validated (i.e.
#    it doesn't already have to exist).  This will be useful for adding
#    a new code.
#
sub _code {
   my($self,$code,$codeset,$no_check_code) = @_;
   $code                    = ''  if (! defined($code));
   $codeset                 = lc($codeset)  if (defined($codeset));

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};
   if ($codeset  &&  ! exists $ALL_CODESETS{$type}{'codesets'}{$codeset}) {
      carp "ERROR: _code: invalid codeset provided: $codeset\n"
        if ($$self{'err'});
      return (1);
   }

   # If no codeset was passed in, return the codeset specified.

   $codeset = $$self{'codeset'}  if (! defined($codeset)  ||  $codeset eq '');
   return (0,'',$codeset)        if ($code eq '');

   # Determine the properties of the codeset

   my($op,@args) = @{ $ALL_CODESETS{$type}{'codesets'}{$codeset} };

   if      ($op eq 'lc') {
      $code = lc($code);
   }

   if ($op eq 'uc') {
      $code = uc($code);
   }

   if ($op eq 'ucfirst') {
      $code = ucfirst(lc($code));
   }

   # uncoverable branch false
   if ($op eq 'numeric') {
      if ($code =~ /^\d+$/) {
         my $l = $args[0];
         $code    = sprintf("%.${l}d", $code);

      } else {
         # uncoverable statement
         carp "ERROR: _code: invalid numeric code: $code\n"  if ($$self{'err'});
         # uncoverable statement
         return (1);
      }
   }

   # Determine if the code is in the codeset.

   if (! $no_check_code  &&
       ! exists $Data{$type}{'code2id'}{$codeset}{$code}  &&
       ! exists $Retired{$type}{$codeset}{'code'}{$code}  &&
       ! exists $Data{$type}{'codealias'}{$codeset}{$code}) {
      carp "ERROR: _code: code not in codeset: $code [$codeset]\n"
        if ($$self{'err'});
      return (1);
   }

   return (0,$code,$codeset);
}

###############################################################################

# $name = $o->code2name(CODE [,CODESET] [,'retired'])
# $code = $o->name2code(NAME [,CODESET] [,'retired'])
#
#    Returns the name associated with the CODE (or vice versa).
#
sub code2name {
   my($self,@args)   = @_;
   my $retired       = 0;
   if (@args  &&  defined($args[$#args])  &&  lc($args[$#args]) eq 'retired') {
      pop(@args);
      $retired       = 1;
   }

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};

   my ($err,$code,$codeset) = $self->_code(@args);
   return undef  if ($err  ||  ! $code);

   $code = $Data{$type}{'codealias'}{$codeset}{$code}
     if (exists $Data{$type}{'codealias'}{$codeset}{$code});

   if (exists $Data{$type}{'code2id'}{$codeset}{$code}) {
      my ($id,$i) = @{ $Data{$type}{'code2id'}{$codeset}{$code} };
      my $name    = $Data{$type}{'id2names'}{$id}[$i];
      return $name;

   } elsif ($retired  &&  exists $Retired{$type}{$codeset}{'code'}{$code}) {
      return $Retired{$type}{$codeset}{'code'}{$code};

   } else {
      return undef;
   }
}

sub name2code {
   my($self,$name,@args)   = @_;
   return undef  if (! $name);
   $name                   = lc($name);

   my $retired       = 0;
   if (@args  &&  defined($args[$#args])  &&  lc($args[$#args]) eq 'retired') {
      pop(@args);
      $retired       = 1;
   }

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};

   my ($err,$tmp,$codeset) = $self->_code('',@args);
   return undef  if ($err);

   if (exists $Data{$type}{'alias2id'}{$name}) {
      my $id = $Data{$type}{'alias2id'}{$name}[0];
      if (exists $Data{$type}{'id2code'}{$codeset}{$id}) {
         return $Data{$type}{'id2code'}{$codeset}{$id};
      }

   } elsif ($retired  &&  exists $Retired{$type}{$codeset}{'name'}{$name}) {
      return $Retired{$type}{$codeset}{'name'}{$name}[0];
   }

   return undef;
}

# $code = $o->code2code(CODE,CODESET2)
# $code = $o->code2code(CODE,CODESET1,CODESET2)
#
#    Changes the code in the CODESET1 (or the current codeset) to another
#    codeset (CODESET2)
#
sub code2code {
   my($self,@args) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};

   my($code,$codeset1,$codeset2,$err);

   if (@args == 2) {
      ($code,$codeset2)      = @args;
      ($err,$code,$codeset1) = $self->_code($code);
      # uncoverable branch true
      return undef  if ($err);

   } elsif (@args == 3) {
      ($code,$codeset1,$codeset2) = @args;
      ($err,$code)                = $self->_code($code,$codeset1);
      return undef  if ($err);
      ($err)                      = $self->_code('',$codeset2);
      # uncoverable branch true
      return undef  if ($err);
   }

   my $name    = $self->code2name($code,$codeset1);
   my $out     = $self->name2code($name,$codeset2);
   return $out;
}

###############################################################################

# @codes = $o->all_codes([CODESET] [,'retired']);
# @names = $o->all_names([CODESET] [,'retired']);
#
#    Returns all codes/names in the specified codeset, including retired
#    ones if the option is given.

sub all_codes {
   my($self,@args)   = @_;
   my $retired       = 0;
   if (@args  &&  defined($args[$#args])  &&  lc($args[$#args]) eq 'retired') {
      pop(@args);
      $retired       = 1;
   }

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};

   my ($err,$tmp,$codeset) = $self->_code('',@args);
   return ()  if ($err);

   my @codes = keys %{ $Data{$type}{'code2id'}{$codeset} };
   push(@codes,keys %{ $Retired{$type}{$codeset}{'code'} })  if ($retired);
   return (sort @codes);
}

sub all_names {
   my($self,@args)   = @_;
   my $retired       = 0;
   if (@args  &&  defined($args[$#args])  &&  lc($args[$#args]) eq 'retired') {
      pop(@args);
      $retired       = 1;
   }

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return (1);
   }
   my $type = $$self{'type'};

   my ($err,$tmp,$codeset) = $self->_code('',@args);
   return ()  if ($err);

   my @codes = $self->all_codes($codeset);
   my @names;

   foreach my $code (@codes) {
      my($id,$i) = @{ $Data{$type}{'code2id'}{$codeset}{$code} };
      my $name   = $Data{$type}{'id2names'}{$id}[$i];
      push(@names,$name);
   }
   if ($retired) {
      foreach my $lc (keys %{ $Retired{$type}{$codeset}{'name'} }) {
         my $name = $Retired{$type}{$codeset}{'name'}{$lc}[1];
         push @names,$name;
      }
   }
   return (sort @names);
}

###############################################################################

# $flag = $o->rename_code (CODE,NEW_NAME [,CODESET])
#
# Change the official name for a code. The original is retained
# as an alias, but the new name will be returned if you lookup the
# name from code.
#
# Returns 1 on success.
#
sub rename_code {
   my($self,$code,$new_name,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure $code/$codeset are both valid

   my($err,$c,$cs) = $self->_code($code,$codeset);
   if ($err) {
      carp "ERROR: rename: Unknown code/codeset: $code [$codeset]\n"
        if ($$self{'err'});
      return 0;
   }
   ($code,$codeset) = ($c,$cs);

   # Cases:
   #   1. Renaming to a name which exists with a different ID
   #      Error
   #
   #   2. Renaming to a name which exists with the same ID
   #      Just change code2id (I value)
   #
   #   3. Renaming to a new name
   #      Create a new alias
   #      Change code2id (I value)

   my $id = $Data{$type}{'code2id'}{$codeset}{$code}[0];

   if (exists $Data{$type}{'alias2id'}{lc($new_name)}) {
      # Existing name (case 1 and 2)

      my ($new_id,$i) = @{ $Data{$type}{'alias2id'}{lc($new_name)} };
      if ($new_id != $id) {
         # Case 1
         carp "ERROR: rename: rename to an existing name not allowed\n"
           if ($$self{'err'});
         return 0;
      }

      # Case 2

      $Data{$type}{'code2id'}{$codeset}{$code}[1] = $i;

   } else {

      # Case 3

      push @{ $Data{$type}{'id2names'}{$id} },$new_name;
      my $i = $#{ $Data{$type}{'id2names'}{$id} };
      $Data{$type}{'alias2id'}{lc($new_name)} = [ $id,$i ];
      $Data{$type}{'code2id'}{$codeset}{$code}[1] = $i;
   }

   return 1;
}

###############################################################################

# $flag = $o->add_code (CODE,NAME [,CODESET])
#
# Add a new code to the codeset. Both CODE and NAME must be
# unused in the code set.
#
sub add_code {
   my($self,$code,$name,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure that $codeset is valid.

   my($err,$c,$cs) = $self->_code($code,$codeset,1);
   if ($err) {
      carp "ERROR: rename: Unknown codeset: $codeset\n"
        if ($$self{'err'});
      return 0;
   }
  ($code,$codeset) = ($c,$cs);

   # Check that $code is unused.

   if (exists $Data{$type}{'code2id'}{$codeset}{$code}  ||
       exists $Data{$type}{'codealias'}{$codeset}{$code}) {
      carp "add_code: code already in use: $code\n"  if ($$self{'err'});
      return 0;
   }

   # Check to see that $name is unused in this code set.  If it is
   # used (but not in this code set), we'll use that ID.  Otherwise,
   # we'll need to get the next available ID.

   my ($id,$i);
   if (exists $Data{$type}{'alias2id'}{lc($name)}) {
      ($id,$i) = @{ $Data{$type}{'alias2id'}{lc($name)} };
      if (exists $Data{$type}{'id2code'}{$codeset}{$id}) {
         carp "add_code: name already in use: $name\n"  if ($$self{'err'});
         return 0;
      }

   } else {
      $id = $Data{$type}{'id'}++;
      $i  = 0;
      $Data{$type}{'alias2id'}{lc($name)} = [ $id,$i ];
      $Data{$type}{'id2names'}{$id}       = [ $name ];
   }

   # Add the new code

   $Data{$type}{'code2id'}{$codeset}{$code} = [ $id,$i ];
   $Data{$type}{'id2code'}{$codeset}{$id}   = $code;

   return 1;
}

###############################################################################

# $flag = $o->delete_code (CODE [,CODESET])
#
# Delete a code from the codeset.
#
sub delete_code {
   my($self,$code,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure $code/$codeset are both valid

   my($err,$c,$cs) = $self->_code($code,$codeset);
   # uncoverable branch true
   if ($err) {
      # uncoverable statement
      carp "ERROR: rename: Unknown code/codeset: $code [$codeset]\n"
        if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   ($code,$codeset) = ($c,$cs);

   # Delete the code

   my $id = $Data{$type}{'code2id'}{$codeset}{$code}[0];
   delete $Data{$type}{'code2id'}{$codeset}{$code};
   delete $Data{$type}{'id2code'}{$codeset}{$id};

   # Delete any aliases that are linked to this code

   foreach my $alias (keys %{ $Data{$type}{'codealias'}{$codeset} }) {
      next  if ($Data{$type}{'codealias'}{$codeset}{$alias} ne $code);
      delete $Data{$type}{'codealias'}{$codeset}{$alias};
   }

   # If this ID is not used in any other codeset, delete it completely.

   foreach my $c (keys %{ $Data{$type}{'id2code'} }) {
      return 1  if (exists $Data{$type}{'id2code'}{$c}{$id});
   }

   my @names = @{ $Data{$type}{'id2names'}{$id} };
   delete $Data{$type}{'id2names'}{$id};

   foreach my $name (@names) {
      delete $Data{$type}{'alias2id'}{lc($name)};
   }

   return 1;
}

###############################################################################

# $flag = $o->add_alias (NAME,NEW_NAME)
#
# Add a new alias. NAME must exist, and NEW_NAME must be unused.
#
sub add_alias {
   my($self,$name,$new_name) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Check that $name is used and $new_name is new.

   my($id);
   if (exists $Data{$type}{'alias2id'}{lc($name)}) {
      $id = $Data{$type}{'alias2id'}{lc($name)}[0];
   } else {
      carp "add_alias: name does not exist: $name\n"  if ($$self{'err'});
      return 0;
   }

   if (exists $Data{$type}{'alias2id'}{lc($new_name)}) {
      carp "add_alias: alias already in use: $new_name\n"  if ($$self{'err'});
      return 0;
   }

   # Add the new alias

   push @{ $Data{$type}{'id2names'}{$id} },$new_name;
   my $i = $#{ $Data{$type}{'id2names'}{$id} };
   $Data{$type}{'alias2id'}{lc($new_name)} = [ $id,$i ];

   return 1;
}

###############################################################################

# $flag = $o->delete_alias (NAME)
#
# This deletes a name from the list of names used by an element.
# NAME must be used, but must NOT be the only name in the list.
#
# Any id2name that references this name will be changed to
# refer to the first name in the list.
#
sub delete_alias {
   my($self,$name) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Check that $name is used.

   my($id,$i);
   if (exists $Data{$type}{'alias2id'}{lc($name)}) {
      ($id,$i) = @{ $Data{$type}{'alias2id'}{lc($name)} };
   } else {
      carp "delete_alias: name does not exist: $name\n"  if ($$self{'err'});
      return 0;
   }

   my $n = $#{ $Data{$type}{'id2names'}{$id} } + 1;
   if ($n == 1) {
      carp "delete_alias: only one name defined (use delete_code instead)\n"
        if ($$self{'err'});
      return 0;
   }

   # Delete the alias.

   splice (@{ $Data{$type}{'id2names'}{$id} },$i,1);
   delete $Data{$type}{'alias2id'}{lc($name)};

   # Every element that refers to this ID:
   #   Ignore     if I < $i
   #   Set to 0   if I = $i
   #   Decrement  if I > $i

   foreach my $codeset (keys %{ $Data{$type}{'code2id'} }) {
      foreach my $code (keys %{ $Data{$type}{'code2id'}{$codeset} }) {
         my($jd,$j) = @{ $Data{$type}{'code2id'}{$codeset}{$code} };
         next  if ($jd ne $id  ||
                   $j < $i);
         if ($i == $j) {
            $Data{$type}{'code2id'}{$codeset}{$code}[1] = 0;
         } else {
            $Data{$type}{'code2id'}{$codeset}{$code}[1]--;
         }
      }
   }

   return 1;
}

###############################################################################

# $flag = $o->replace_code (CODE,NEW_CODE [,CODESET])
#
# Change the official code. The original is retained as an alias, but
# the new code will be returned if do a name2code lookup.
#
sub replace_code {
   my($self,$code,$new_code,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure $code/$codeset are both valid (and that $new_code is the
   # correct format)

   my($err,$c,$cs) = $self->_code($code,$codeset);
   if ($err) {
      carp "ERROR: rename_code: Unknown code/codeset: $code [$codeset]\n"
        if ($$self{'err'});
      return 0;
   }
   ($code,$codeset) = ($c,$cs);

   ($err,$new_code,$codeset) = $self->_code($new_code,$codeset,1);

   # Cases:
   #   1. Renaming code to an existing alias of this code:
   #      Make the alias real and the code an alias
   #
   #   2. Renaming code to some other existing alias:
   #      Error
   #
   #   3. Renaming code to some other code:
   #      Error (
   #
   #   4. Renaming code to a new code:
   #      Make code into an alias
   #      Replace code with new_code.

   if (exists $Data{$type}{'codealias'}{$codeset}{$new_code}) {
      # Cases 1 and 2
      if ($Data{$type}{'codealias'}{$codeset}{$new_code} eq $code) {
         # Case 1

         delete $Data{$type}{'codealias'}{$codeset}{$new_code};

      } else {
         # Case 2
         carp "rename_code: new code already in use: $new_code\n"
           if ($$self{'err'});
         return 0;
      }

   } elsif (exists $Data{$type}{'code2id'}{$codeset}{$new_code}) {
      # Case 3
      carp "rename_code: new code already in use: $new_code\n"
        if ($$self{'err'});
      return 0;
   }

   # Cases 1 and 4

   $Data{$type}{'codealias'}{$codeset}{$code} = $new_code;

   my $id = $Data{$type}{'code2id'}{$codeset}{$code}[0];
   $Data{$type}{'code2id'}{$codeset}{$new_code} =
     $Data{$type}{'code2id'}{$codeset}{$code};
   delete $Data{$type}{'code2id'}{$codeset}{$code};

   $Data{$type}{'id2code'}{$codeset}{$id} = $new_code;

   return 1;
}

###############################################################################

# $flag = $o->add_code_alias (CODE,NEW_CODE [,CODESET])
#
# Adds an alias for the code.
#
sub add_code_alias {
   my($self,$code,$new_code,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure $code/$codeset are both valid and that the new code is
   # properly formatted.

   my($err,$c,$cs) = $self->_code($code,$codeset);
   if ($err) {
      carp "ERROR: add_code_alias: Unknown code/codeset: $code [$codeset]\n"
        if ($$self{'err'});
      return 0;
   }
   ($code,$codeset) = ($c,$cs);

   ($err,$new_code,$cs) = $self->_code($new_code,$codeset,1);

   # Check that $new_code does not exist.

   if (exists $Data{$type}{'code2id'}{$codeset}{$new_code}  ||
       exists $Data{$type}{'codealias'}{$codeset}{$new_code}) {
      # uncoverable branch true
      carp "add_code_alias: code already in use: $new_code\n"  if ($$self{'err'});
      return 0;
   }

   # Add the alias

   $Data{$type}{'codealias'}{$codeset}{$new_code} = $code;

   return 1;
}

###############################################################################

# $flag = $o->delete_code_alias (ALIAS [,CODESET])
#
# Deletes an alias for the code.
#
sub delete_code_alias {
   my($self,$code,$codeset) = @_;

   # uncoverable branch true
   if (! $$self{'type'}) {
      # uncoverable statement
      carp "ERROR: no type set for Locale::Codes object\n"  if ($$self{'err'});
      # uncoverable statement
      return 0;
   }
   my $type = $$self{'type'};

   # Make sure $code/$codeset are both valid

   my($err,$c,$cs) = $self->_code($code,$codeset);
   if ($err) {
      # uncoverable branch true
      carp "ERROR: rename: Unknown code/codeset: $code [$codeset]\n"
        if ($$self{'err'});
      return 0;
   }
   ($code,$codeset) = ($c,$cs);

   # Check that $code exists in the codeset as an alias.

   if (! exists $Data{$type}{'codealias'}{$codeset}{$code}) {
      # uncoverable branch true
      carp "delete_code_alias(): no alias defined: $code\n"  if ($$self{'err'});
      return 0;
   }

   # Delete the alias

   delete $Data{$type}{'codealias'}{$codeset}{$code};

   return 1;
}

1;
# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# cperl-indent-level: 3
# cperl-continued-statement-offset: 2
# cperl-continued-brace-offset: 0
# cperl-brace-offset: 0
# cperl-brace-imaginary-offset: 0
# cperl-label-offset: 0
# End: