latin1.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. /*
  4. * The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
  5. * prefix of latintab[j].ld only when j<i.
  6. */
  7. struct cvlist
  8. {
  9. char *ld; /* must be seen before using this conversion */
  10. char *si; /* options for last input characters */
  11. Rune *so; /* the corresponding Rune for each si entry */
  12. } latintab[] = {
  13. #include "../port/latin1.h"
  14. 0, 0, 0
  15. };
  16. /*
  17. * Given n characters k[0]..k[n-1], find the rune or return -1 for failure.
  18. */
  19. long
  20. unicode(Rune *k, int n)
  21. {
  22. long c;
  23. Rune *r;
  24. c = 0;
  25. for(r = &k[1]; r<&k[n]; r++){ /* +1 to skip [Xx] */
  26. c <<= 4;
  27. if('0'<=*r && *r<='9')
  28. c += *r-'0';
  29. else if('a'<=*r && *r<='f')
  30. c += 10 + *r-'a';
  31. else if('A'<=*r && *r<='F')
  32. c += 10 + *r-'A';
  33. else
  34. return -1;
  35. }
  36. return c;
  37. }
  38. /*
  39. * Given n characters k[0]..k[n-1], find the corresponding rune or return -1 for
  40. * failure, or something < -1 if n is too small. In the latter case, the result
  41. * is minus the required n.
  42. */
  43. long
  44. latin1(Rune *k, int n)
  45. {
  46. struct cvlist *l;
  47. int c;
  48. char* p;
  49. if(k[0] == 'X')
  50. if(n>=5)
  51. return unicode(k, 5);
  52. else
  53. return -5;
  54. if(k[0] == 'x')
  55. if(n>=UTFmax*2+1)
  56. return unicode(k, UTFmax*2+1);
  57. else
  58. return -(UTFmax+1);
  59. for(l=latintab; l->ld!=0; l++)
  60. if(k[0] == l->ld[0]){
  61. if(n == 1)
  62. return -2;
  63. if(l->ld[1] == 0)
  64. c = k[1];
  65. else if(l->ld[1] != k[1])
  66. continue;
  67. else if(n == 2)
  68. return -3;
  69. else
  70. c = k[2];
  71. for(p=l->si; *p!=0; p++)
  72. if(*p == c)
  73. return l->so[p - l->si];
  74. return -1;
  75. }
  76. return -1;
  77. }