本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2KB

  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package token
  5. type serializedFile struct {
  6. // fields correspond 1:1 to fields with same (lower-case) name in File
  7. Name string
  8. Base int
  9. Size int
  10. Lines []int
  11. Infos []lineInfo
  12. }
  13. type serializedFileSet struct {
  14. Base int
  15. Files []serializedFile
  16. }
  17. // Read calls decode to deserialize a file set into s; s must not be nil.
  18. func (s *FileSet) Read(decode func(interface{}) error) error {
  19. var ss serializedFileSet
  20. if err := decode(&ss); err != nil {
  21. return err
  22. }
  23. s.mutex.Lock()
  24. s.base = ss.Base
  25. files := make([]*File, len(ss.Files))
  26. for i := 0; i < len(ss.Files); i++ {
  27. f := &ss.Files[i]
  28. files[i] = &File{s, f.Name, f.Base, f.Size, f.Lines, f.Infos}
  29. }
  30. s.files = files
  31. s.last = nil
  32. s.mutex.Unlock()
  33. return nil
  34. }
  35. // Write calls encode to serialize the file set s.
  36. func (s *FileSet) Write(encode func(interface{}) error) error {
  37. var ss serializedFileSet
  38. s.mutex.Lock()
  39. ss.Base = s.base
  40. files := make([]serializedFile, len(s.files))
  41. for i, f := range s.files {
  42. files[i] = serializedFile{f.name, f.base, f.size, f.lines, f.infos}
  43. }
  44. ss.Files = files
  45. s.mutex.Unlock()
  46. return encode(ss)
  47. }
上海开阖软件有限公司 沪ICP备12045867号-1