util.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include "dat.h"
  11. #include "fns.h"
  12. void
  13. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  14. {
  15. uchar *q;
  16. Rune *s;
  17. int j, w;
  18. /*
  19. * Always guaranteed that n bytes may be interpreted
  20. * without worrying about partial runes. This may mean
  21. * reading up to UTFmax-1 more bytes than n; the caller
  22. * knows this. If n is a firm limit, the caller should
  23. * set p[n] = 0.
  24. */
  25. q = (uchar*)p;
  26. s = r;
  27. for(j=0; j<n; j+=w){
  28. if(*q < Runeself){
  29. w = 1;
  30. *s = *q++;
  31. }else{
  32. w = chartorune(s, (char*)q);
  33. q += w;
  34. }
  35. if(*s)
  36. s++;
  37. else if(nulls)
  38. *nulls = TRUE;
  39. }
  40. *nb = (char*)q-p;
  41. *nr = s-r;
  42. }
  43. void
  44. error(char *s)
  45. {
  46. fprint(2, "rio: %s: %r\n", s);
  47. if(errorshouldabort)
  48. abort();
  49. threadexitsall("error");
  50. }
  51. void*
  52. erealloc(void *p, uint n)
  53. {
  54. p = realloc(p, n);
  55. if(p == nil)
  56. error("realloc failed");
  57. return p;
  58. }
  59. void*
  60. emalloc(uint n)
  61. {
  62. void *p;
  63. p = malloc(n);
  64. if(p == nil)
  65. error("malloc failed");
  66. memset(p, 0, n);
  67. return p;
  68. }
  69. char*
  70. estrdup(char *s)
  71. {
  72. char *p;
  73. p = malloc(strlen(s)+1);
  74. if(p == nil)
  75. error("strdup failed");
  76. strcpy(p, s);
  77. return p;
  78. }
  79. int
  80. isalnum(Rune c)
  81. {
  82. /*
  83. * Hard to get absolutely right. Use what we know about ASCII
  84. * and assume anything above the Latin control characters is
  85. * potentially an alphanumeric.
  86. */
  87. if(c <= ' ')
  88. return FALSE;
  89. if(0x7F<=c && c<=0xA0)
  90. return FALSE;
  91. if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
  92. return FALSE;
  93. return TRUE;
  94. }
  95. Rune*
  96. strrune(Rune *s, Rune c)
  97. {
  98. Rune c1;
  99. if(c == 0) {
  100. while(*s++)
  101. ;
  102. return s-1;
  103. }
  104. while(c1 = *s++)
  105. if(c1 == c)
  106. return s-1;
  107. return nil;
  108. }
  109. int
  110. min(int a, int b)
  111. {
  112. if(a < b)
  113. return a;
  114. return b;
  115. }
  116. int
  117. max(int a, int b)
  118. {
  119. if(a > b)
  120. return a;
  121. return b;
  122. }
  123. char*
  124. runetobyte(Rune *r, int n, int *ip)
  125. {
  126. char *s;
  127. int m;
  128. s = emalloc(n*UTFmax+1);
  129. m = snprint(s, n*UTFmax+1, "%.*S", n, r);
  130. *ip = m;
  131. return s;
  132. }