__res.pyx 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from _codecs import utf_8_decode, utf_8_encode
  2. from libcpp cimport bool
  3. from util.generic.string cimport TString, TStringBuf
  4. cdef extern from "library/cpp/resource/resource.h" namespace "NResource":
  5. cdef bool Has(const TStringBuf key) except +
  6. cdef size_t Count() except +
  7. cdef TStringBuf KeyByIndex(size_t idx) except +
  8. cdef bool FindExact(const TStringBuf key, TString* result) nogil except +
  9. def count():
  10. return Count()
  11. def key_by_index(idx):
  12. cdef TStringBuf ret = KeyByIndex(idx)
  13. return ret.Data()[:ret.Size()]
  14. def find(s):
  15. cdef TString res
  16. if isinstance(s, str):
  17. s = utf_8_encode(s)[0]
  18. if FindExact(TStringBuf(s, len(s)), &res):
  19. return res.c_str()[:res.length()]
  20. return None
  21. def has(s):
  22. if isinstance(s, str):
  23. s = utf_8_encode(s)[0]
  24. return Has(s)
  25. include "importer.pxi"