os.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package uaparser
  2. type Os struct {
  3. Family string
  4. Major string
  5. Minor string
  6. Patch string
  7. PatchMinor string `yaml:"patch_minor"`
  8. }
  9. func (parser *osParser) Match(line string, os *Os) {
  10. matches := parser.Reg.FindStringSubmatchIndex(line)
  11. if len(matches) > 0 {
  12. os.Family = string(parser.Reg.ExpandString(nil, parser.OSReplacement, line, matches))
  13. os.Major = string(parser.Reg.ExpandString(nil, parser.V1Replacement, line, matches))
  14. os.Minor = string(parser.Reg.ExpandString(nil, parser.V2Replacement, line, matches))
  15. os.Patch = string(parser.Reg.ExpandString(nil, parser.V3Replacement, line, matches))
  16. os.PatchMinor = string(parser.Reg.ExpandString(nil, parser.V4Replacement, line, matches))
  17. }
  18. }
  19. func (os *Os) ToString() string {
  20. var str string
  21. if os.Family != "" {
  22. str += os.Family
  23. }
  24. version := os.ToVersionString()
  25. if version != "" {
  26. str += " " + version
  27. }
  28. return str
  29. }
  30. func (os *Os) ToVersionString() string {
  31. var version string
  32. if os.Major != "" {
  33. version += os.Major
  34. }
  35. if os.Minor != "" {
  36. version += "." + os.Minor
  37. }
  38. if os.Patch != "" {
  39. version += "." + os.Patch
  40. }
  41. if os.PatchMinor != "" {
  42. version += "." + os.PatchMinor
  43. }
  44. return version
  45. }