CFString.odin 5.5 KB

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