2
0

CFString.odin 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package CoreFoundation
  2. foreign import CoreFoundation "system:CoreFoundation.framework"
  3. String :: distinct TypeRef // same as CFStringRef
  4. StringEncoding :: distinct u32
  5. StringBuiltInEncodings :: enum StringEncoding {
  6. MacRoman = 0,
  7. WindowsLatin1 = 0x0500,
  8. ISOLatin1 = 0x0201,
  9. NextStepLatin = 0x0B01,
  10. ASCII = 0x0600,
  11. Unicode = 0x0100,
  12. UTF8 = 0x08000100,
  13. NonLossyASCII = 0x0BFF,
  14. UTF16 = 0x0100,
  15. UTF16BE = 0x10000100,
  16. UTF16LE = 0x14000100,
  17. UTF32 = 0x0c000100,
  18. UTF32BE = 0x18000100,
  19. UTF32LE = 0x1c000100,
  20. }
  21. StringEncodings :: enum Index {
  22. MacJapanese = 1,
  23. MacChineseTrad = 2,
  24. MacKorean = 3,
  25. MacArabic = 4,
  26. MacHebrew = 5,
  27. MacGreek = 6,
  28. MacCyrillic = 7,
  29. MacDevanagari = 9,
  30. MacGurmukhi = 10,
  31. MacGujarati = 11,
  32. MacOriya = 12,
  33. MacBengali = 13,
  34. MacTamil = 14,
  35. MacTelugu = 15,
  36. MacKannada = 16,
  37. MacMalayalam = 17,
  38. MacSinhalese = 18,
  39. MacBurmese = 19,
  40. MacKhmer = 20,
  41. MacThai = 21,
  42. MacLaotian = 22,
  43. MacGeorgian = 23,
  44. MacArmenian = 24,
  45. MacChineseSimp = 25,
  46. MacTibetan = 26,
  47. MacMongolian = 27,
  48. MacEthiopic = 28,
  49. MacCentralEurRoman = 29,
  50. MacVietnamese = 30,
  51. MacExtArabic = 31,
  52. MacSymbol = 33,
  53. MacDingbats = 34,
  54. MacTurkish = 35,
  55. MacCroatian = 36,
  56. MacIcelandic = 37,
  57. MacRomanian = 38,
  58. MacCeltic = 39,
  59. MacGaelic = 40,
  60. MacFarsi = 0x8C,
  61. MacUkrainian = 0x98,
  62. MacInuit = 0xEC,
  63. MacVT100 = 0xFC,
  64. MacHFS = 0xFF,
  65. ISOLatin2 = 0x0202,
  66. ISOLatin3 = 0x0203,
  67. ISOLatin4 = 0x0204,
  68. ISOLatinCyrillic = 0x0205,
  69. ISOLatinArabic = 0x0206,
  70. ISOLatinGreek = 0x0207,
  71. ISOLatinHebrew = 0x0208,
  72. ISOLatin5 = 0x0209,
  73. ISOLatin6 = 0x020A,
  74. ISOLatinThai = 0x020B,
  75. ISOLatin7 = 0x020D,
  76. ISOLatin8 = 0x020E,
  77. ISOLatin9 = 0x020F,
  78. ISOLatin10 = 0x0210,
  79. DOSLatinUS = 0x0400,
  80. DOSGreek = 0x0405,
  81. DOSBalticRim = 0x0406,
  82. DOSLatin1 = 0x0410,
  83. DOSGreek1 = 0x0411,
  84. DOSLatin2 = 0x0412,
  85. DOSCyrillic = 0x0413,
  86. DOSTurkish = 0x0414,
  87. DOSPortuguese = 0x0415,
  88. DOSIcelandic = 0x0416,
  89. DOSHebrew = 0x0417,
  90. DOSCanadianFrench = 0x0418,
  91. DOSArabic = 0x0419,
  92. DOSNordic = 0x041A,
  93. DOSRussian = 0x041B,
  94. DOSGreek2 = 0x041C,
  95. DOSThai = 0x041D,
  96. DOSJapanese = 0x0420,
  97. DOSChineseSimplif = 0x0421,
  98. DOSKorean = 0x0422,
  99. DOSChineseTrad = 0x0423,
  100. WindowsLatin2 = 0x0501,
  101. WindowsCyrillic = 0x0502,
  102. WindowsGreek = 0x0503,
  103. WindowsLatin5 = 0x0504,
  104. WindowsHebrew = 0x0505,
  105. WindowsArabic = 0x0506,
  106. WindowsBalticRim = 0x0507,
  107. WindowsVietnamese = 0x0508,
  108. WindowsKoreanJohab = 0x0510,
  109. ANSEL = 0x0601,
  110. JIS_X0201_76 = 0x0620,
  111. JIS_X0208_83 = 0x0621,
  112. JIS_X0208_90 = 0x0622,
  113. JIS_X0212_90 = 0x0623,
  114. JIS_C6226_78 = 0x0624,
  115. ShiftJIS_X0213 = 0x0628,
  116. ShiftJIS_X0213_MenKuTen = 0x0629,
  117. GB_2312_80 = 0x0630,
  118. GBK_95 = 0x0631,
  119. GB_18030_2000 = 0x0632,
  120. KSC_5601_87 = 0x0640,
  121. KSC_5601_92_Johab = 0x0641,
  122. CNS_11643_92_P1 = 0x0651,
  123. CNS_11643_92_P2 = 0x0652,
  124. CNS_11643_92_P3 = 0x0653,
  125. ISO_2022_JP = 0x0820,
  126. ISO_2022_JP_2 = 0x0821,
  127. ISO_2022_JP_1 = 0x0822,
  128. ISO_2022_JP_3 = 0x0823,
  129. ISO_2022_CN = 0x0830,
  130. ISO_2022_CN_EXT = 0x0831,
  131. ISO_2022_KR = 0x0840,
  132. EUC_JP = 0x0920,
  133. EUC_CN = 0x0930,
  134. EUC_TW = 0x0931,
  135. EUC_KR = 0x0940,
  136. ShiftJIS = 0x0A01,
  137. KOI8_R = 0x0A02,
  138. Big5 = 0x0A03,
  139. MacRomanLatin1 = 0x0A04,
  140. HZ_GB_2312 = 0x0A05,
  141. Big5_HKSCS_1999 = 0x0A06,
  142. VISCII = 0x0A07,
  143. KOI8_U = 0x0A08,
  144. Big5_E = 0x0A09,
  145. NextStepJapanese = 0x0B02,
  146. EBCDIC_US = 0x0C01,
  147. EBCDIC_CP037 = 0x0C02,
  148. UTF7 = 0x04000100,
  149. UTF7_IMAP = 0x0A10,
  150. ShiftJIS_X0213_00 = 0x0628, // Deprecated. Use `ShiftJIS_X0213` instead.
  151. }
  152. @(link_prefix="CF", default_calling_convention="c")
  153. foreign CoreFoundation {
  154. // Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.
  155. StringGetCString :: proc(theString: String, buffer: [^]byte, bufferSize: Index, encoding: StringEncoding) -> b8 ---
  156. // Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a string.
  157. StringGetLength :: proc(theString: String) -> Index ---
  158. // Returns the maximum number of bytes a string of a specified length (in Unicode characters) will take up if encoded in a specified encoding.
  159. StringGetMaximumSizeForEncoding :: proc(length: Index, encoding: StringEncoding) -> Index ---
  160. // Fetches a range of the characters from a string into a byte buffer after converting the characters to a specified encoding.
  161. StringGetBytes :: proc(thestring: String, range: Range, encoding: StringEncoding, lossByte: u8, isExternalRepresentation: b8, buffer: [^]byte, maxBufLen: Index, usedBufLen: ^Index) -> Index ---
  162. StringIsEncodingAvailable :: proc(encoding: StringEncoding) -> bool ---
  163. @(link_name = "__CFStringMakeConstantString")
  164. StringMakeConstantString :: proc "c" (#const c: cstring) -> String ---
  165. }
  166. STR :: StringMakeConstantString
  167. StringCopyToOdinString :: proc(theString: String, allocator := context.allocator) -> (str: string, ok: bool) #optional_ok {
  168. length := StringGetLength(theString)
  169. max := StringGetMaximumSizeForEncoding(length, StringEncoding(StringBuiltInEncodings.UTF8))
  170. buf, err := make([]byte, max, allocator)
  171. if err != nil {
  172. return
  173. }
  174. n: Index
  175. StringGetBytes(theString, {0, length}, StringEncoding(StringBuiltInEncodings.UTF8), 0, false, raw_data(buf), Index(len(buf)), &n)
  176. return string(buf[:n]), true
  177. }