strings.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package ndr
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. const (
  7. subStringArrayTag = `ndr:"varying,X-subStringArray"`
  8. subStringArrayValue = "X-subStringArray"
  9. )
  10. func uint16SliceToString(a []uint16) string {
  11. s := make([]rune, len(a), len(a))
  12. for i := range s {
  13. s[i] = rune(a[i])
  14. }
  15. if len(s) > 0 {
  16. // Remove any null terminator
  17. if s[len(s)-1] == rune(0) {
  18. s = s[:len(s)-1]
  19. }
  20. }
  21. return string(s)
  22. }
  23. func (dec *Decoder) readVaryingString(def *[]deferedPtr) (string, error) {
  24. a := new([]uint16)
  25. v := reflect.ValueOf(a)
  26. var t reflect.StructTag
  27. err := dec.fillUniDimensionalVaryingArray(v.Elem(), t, def)
  28. if err != nil {
  29. return "", err
  30. }
  31. s := uint16SliceToString(*a)
  32. return s, nil
  33. }
  34. func (dec *Decoder) readConformantVaryingString(def *[]deferedPtr) (string, error) {
  35. a := new([]uint16)
  36. v := reflect.ValueOf(a)
  37. var t reflect.StructTag
  38. err := dec.fillUniDimensionalConformantVaryingArray(v.Elem(), t, def)
  39. if err != nil {
  40. return "", err
  41. }
  42. s := uint16SliceToString(*a)
  43. return s, nil
  44. }
  45. func (dec *Decoder) readStringsArray(v reflect.Value, tag reflect.StructTag, def *[]deferedPtr) error {
  46. d, _ := sliceDimensions(v.Type())
  47. ndrTag := parseTags(tag)
  48. var m []int
  49. //var ms int
  50. if ndrTag.HasValue(TagConformant) {
  51. for i := 0; i < d; i++ {
  52. m = append(m, int(dec.precedingMax()))
  53. }
  54. //common max size
  55. _ = dec.precedingMax()
  56. //ms = int(n)
  57. }
  58. tag = reflect.StructTag(subStringArrayTag)
  59. err := dec.fillVaryingArray(v, tag, def)
  60. if err != nil {
  61. return fmt.Errorf("could not read string array: %v", err)
  62. }
  63. return nil
  64. }